A quick post (based on a google search) about WCParam
I was reviewing my logs and noticed that two people happened to hit the site looking for an explanation on how to add a parameter to WCParam. So I thought I'd take a minute or two and write up a quick example using JSTL.
What you need to do is add the parameter in your c:url tag as a parameter when you define it. Let's say that I have a value that I am taking from a c:forEach loop (with a var="items"), we'll call it 'otherId' (since I'm going to pass this through the location/address-bar). I am also going to pass a langId (language code), a storeId (we're all about commerce), a catalogId (yep, we like to sell things), and finally my otherId. Using the code below (and yes I'm skipping setting up your command or whatever you are passing to) I can set up a quick c:url tag with my custom parameter in it that I can then access through the ${WCParam.otherId} on the page that I go to.
You must be using the 'c' tag lib for this to work, so don't forget to include that on your page, here we go.
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <c:url var="myURL" context="${CommandContext.webpath}" value="/mycmd"> <c:param name="langId" value="${langId}" /> <c:param name="storeId" value="${WCParam.storeId}" /> <c:param name="catalogId" value="${WCParam.catalogId}" /> <c:param name="otherId" value="${items.otherId}" /> </c:url> |
And that, in a quick nut shell is how you get a variable accessible through your WCParam. Remember, WCParam is pretty much the same as saying, 'grab it from the location/address/url-bar'.
Feel free to drop corrections and comments (and yes I realize that my code box says HTML4STRICT, until it supports JSP code, HTML4STRICT is what you get).
Print This Post
JSTL Agent identification
So converting NT2.2 to be IE6 friendly was a kick in the teeth... and as Dustin posted earlier, it would have been nice to throw up a warning window and let them see the horror or an IE6 rendering job. However we coded it up to be usable in IE6 and in the process had to rewrite some of the homepage. At first I used the ugly <![if IE 6]> // do something magical <![endif]--> tags, but they caused Safari to explode on itself. So I went back did a quick study and came out with a JSTL solution, and then modded it with Drew to get it working.
Using JSTL you can get the browser through the user-agent string in the URL request header. Like this:
<c:set var="browser" value="${header['User-Agent']}" scope="page"/> |
You can see that I used a c:set to get the 'User-Agent' string and set the variable 'browser' to that value.
Now we just need to set up a c:choose to determine the path of IE 6 or other browser types using the indexOf function in the JSTL 'fn' library. So a quick c:choose statement would look like this.
<c:set var="browser" value="${header['User-Agent']}" scope="page"/> <c:choose> <c:when test="${!empty browser && fn:indexOf(browser, 'MSIE 6') >= 0}"> NOT EMPTY </c:when> <c:otherwise> EMPTY </c:otherwise> </c:choose> |
In the 'c:when test' you can see that the indexOf ends with a '>=0', this is because indexOf returns an INT, which is treated Boolean-esque in this case (it's not a Boolean, but the way we are using it resembles a Boolean).
Don't forget that when using the JSTL fn and c tag libraries you need to include them in your jsp file. use the following at the top of your page to use these excellent tag-libs.
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> |
If you have any questions, leave'em in the comments
.
Print This Post
Loop through nothing.
We all know that using jstl we can us a <c:forEach...> to loop through just about anything, for example in the new NT2.2 we used a loop through all SKUs to grab gallery images for all apparel products.
<c:forEach var="items" items="${product.publishedItems}"> |
This is great but required all publishedItems in order for the loop to be effective.
But what if you want to loop through nothing? I know I know, sounds like it could be pretty useless but it's not. What I'm talking about is what if you want to run a loop 5 times but have nothing to loop through? Well it's pretty simple but difficult to find any documentation on, hence this post. So before you go dropping scriplet (or as I like to call it, "Nava" or "NinJava") code on your .jsp and have one of your teammates commit treason, read on Josephine, read on.
Brace yourself, here it is.
<c:forEach begin="1" end="8"> |
Pretty neat huh, your loop starts at one and ends at 8, one thing I've yet to find is an effective way to break the loop using jstl if necessary. Ninja said something about setting a flag and I did that but at second glance it's not quite as effective as I had originally hoped.
Print This Post
The joys of Try Catch (in a jsp)
This post is brought about after having Ninja come over and look at my code and making me realize that while what I was doing was good, I had not used a try catch and as such would never find exactly why my code was generating over 1500 lines of errors in my server log (ouch). Here is the way of Scriptlet (aka ninjascript) Try Catch-ery.
<% try { %>; <%-- code goes here --%>; <% } catch (Exception e) { e.printStackTrace(); Thread.sleep(20000); } %>; |
You can further mod this to make it possibly more helpful by adding/replacing the last line so you end up with something like:
<% try { %> <%-- Code goes here --%> <% } catch (Exception e) { System.out.println("Exception caught in NameOf.jsp"); System.out.println(e.toString()); e.printStackTrace(); } %> |
Now for those people who are purists and understand that scriptlets really are not up to par, and that they are ugly and screw things up and are not elegant in the least, there is another way to Try Catch on your JSP. Simply use a JSTL Try Catch. Here I'll set up an example:
<c:catch var ="catchException"> <wcbase:useBean id="category" classname="com.ibm.commerce.catalog.beans.CategoryDataBean" > <c:set value="${WCParam.parent_category_rn}" target="${category}" property="categoryId" /> </wcbase:useBean> </c:catch> <c:if test = "${catchException!=null}"> The exception is : ${catchException}<br /><br/> There is an exception: ${catchException.message}<br/> </c:if> |
We set up a variable (catchException) and then what we want to do inside that c:catch. So we are trying to instantiate this bean and we want to make sure it doesn't throw an error (which as part of the joys of websphere if that bean throws an error your page will fail and you'll either get a portion of your page with nothing else, or you'll get a series of dreaded CMNxxxx errors). Right after the c:catch block we set up a c:if with to test value of ${catchException, if it's not empty, then an error was thrown and we should kick out what the error is so that we can fix it (without digging through 1500 lines of error-logged code).
Print This Post
Dirty Auto-Eval of Rational in 561
So your compy has gone lag-happy, and it's all because of the auto syntax-error-warning checker and it's driving you nuts. Well no longer! Simply crack open your window->preferences and then scroll down to "Web and XML Files" expand that to "JSP Files", and then click on "JSP Annotations". On this click on warnings (over to the left) and un-check the show in text. That should fix your problems... Now code in peace, and enjoy!
Print This Post
JSTL to the rescue when your EJB S’plodes
Alright, so I tried using a nice wcbase: call to get the bean I wanted to I could grab the SKU, but that didn't work. Instead I ended up searching through some old IBM 561 docs until I found that I could pull an item number from catEntry. The main problem was that the number I pulled was exactly what I asked for, the item from the database (I wanted the SKU). I read a little more down the list and found that I could get the catEntry.partNumber which does give me the SKU, but it gives me the prd_ in front of it. So I checked with Mr. Google, and he showed me this site from ONJava.com that had some JSTL goodery. Most of the page was not important, but what was useful was the substringAfter command. Here are some JSTL goodies you can use in a pinch to fix a problem (and yes I realize this fix will NOT work on NordicTrack as we are incorporating apparel and that has tons of SKU's for clothing, and this fix actually does not fetch the actual SKU, but a close representation of it).
fn:substring(string, begin, end) | Returns a part of the string, starting from the begin index up to and including the end index.
fn:substringAfter(string, substring) | Returns the part of the string that follows the substring.
fn:substringBefore(string, substring) | Returns the part of the string that precedes the substring.
I ended up using the substringAfter like this --> SKU: <c:out value="${fn:substringAfter(catEntry.partNumber, 'prd_')}" /> so that I could get the prd number and then trim/substring off the prd_. I now need to go back and trim off the _21663 on a few NT products...
Print This Post