ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

JSP Standard Tag Libraries, Part 2
Pages: 1, 2, 3

Iteration Actions

Probably the most useful features of the JSTL are the iteration actions. The actions that support iterations are <c:forEach>, <c:forToken>, and <x:forEach>. There are also XML tags that mimic the core tags for iterations. We'll talk about the XML action tag in a moment, but for now, let's stick with the core actions.



These actions support all of the standard J2SE collection types, including List, LinkedList, ArrayList, Vector, Stack, and Set. This also includes java.util.Map objects like HashMap, Hashtable, Properties, Provider, and Attributes. You can also iterate over an array of Objects or primitive types. When using primitive types, the current item is wrapped with the Java wrapper class. So the current item in an Array of ints would be an Integer. Two objects are exported for each iteration, the current item and the iteration status. Let's look at the sample below:


<table>
<c:forEach var="product" 
              items="${products}" 
              varStatus="status">
<tr>
	<td><c:out value="${status.count}"/></td>
	<td><c:out value="${product.name}"/></td>
</tr>
</c:forEach>
</table>

In this example, the Collection is specified by the EL as products. The current item is contained in the variable product. The current status of the iteration is held in the varStatus object status. Pretty simple.

URL Actions

In addition to the iteration actions, the Core library also offers URL-related actions. This includes support for hyperlinks, resource imports, and redirects. Using the <c:url> tag conveniently handles URL rewriting and encoding automatically. If we look at the example of specifying a URL with a parameter and then using it in a link, it might look like this:


<c:url=http://mysite.com/register var="myUrl">
	<c:param name="name" value="${param.name}"/>
</c:url>
<a href='<c:out value="${myUrl}"/>'>Register</a>

Using resource imports also became more powerful with the JSTL. It is now possible to specify absolute, relative, relative URL with a foreign context, and FTP resources. Let's look at a couple of examples:

  • Absolute URL: <c:import url="http://sample.com/Welcome.html"/>
  • Relative URL (to the current context): <c:import url="/copyright.html"/>
  • Relative URL with a foreign context: <c:import url="/myLogo.html" context="/common"/>
  • FTP resource: <c:import url="ftp://ftp.sample.com/myFile"/>

From the examples above, it's obvious that <c:import> provides more power than using a <jsp:include>; however, there are also other reasons to use the tags. There are improvements in resource content buffering in the JSTL that eliminate unnecessary buffering. If you used a <jsp:include> inside of a transformation, the resource content would be read and then the body content would be written to the current JspWriter. The content would then be re-read by the transform tag. When using <c:import>, the body content will only be read in once, and then the data will be processed by the transform tag.

It is also possible to include resource content inline or exported as either a String or Reader Object using attributes called var or varReader. The String Object is reusable and has its contents cached. The advantage of using a Reader is that the content can be accessed directly without any buffering. A Reader, however, needs to be nested within a <c:import> to assure that the Reader is properly closed and isn't accidentally left open. We can demonstrate this with the sample below:


<c:import url=http://sample.com/customers varReader="customers">
	<mytag:process in="${customers}"/>
</c:import>

Internationalization

Another important functional area of the JSTL is its international (I18N) and formatting support. Using the actions provided makes it simple to internationalize on a request or context-configuration basis. These actions make use of the standard J2SE ResourceBundle used to hold the strings that are translated. The JSTL tags will use the appropriate ResourceBundle, depending on the specified locale. The <fmt:setLocale> tag can be used to specify a locale like this: <fmt:setLocale value="es_Es"/>, where the value equals that language and country code. It's also possible to specificy a ResourceBundle: <fmt:bundle basename="ApplicationResource_fr"/>.

Once the locale or the bundle is set, then the <fmt:message> tag will automatically use the correct ResourceBundle. Parametric replacement can be performed on strings like so:


<fmt:message key="welcome">
	<fmt:param value="${visitCount}" />
<fmt:message/>

You can also use < fmt:requestEncoding/> to set the request's character encoding.

Getting and displaying strings is only half the international fun. There also must be a way to format and parse numbers and dates; different locales typically have specific ways of doing so. Using <fmt:formatNumber> or <fmt:parseNumber> allows for the formatting of numbers, currencies, and percentages according to the current Locale. Patterns can also be specified by using the pattern attribute. To demonstrate: <fmt:formatNumber value="12.3" pattern=".00"/> would output "12.30."

Dates and times are handled using the <fmt:formatDate>, <fmt:timeZone>, <fmt:setTimeZone>, and <fmt:parseDate>.

Pages: 1, 2, 3

Next Pagearrow