JDev

Code, jstlJuly 4, 2006 7:57 pm

The simplest way to format a date type field in JSTL is to use a formatDate tag. Example:

<fmt:formatDate value="${row[1]}"/>

 This will display a date value in the default format which is MMM-D-YYYY on my Windows XP machine. You can use various attributes such as dateStyle, timeStyle and pattern to customize the date display format.

You can use any of the values default, short, medium, long, or full for the dateStyle attribute.

 This is how you format both date and time using the current date:

 <jsp:useBean id="now" class="java.util.Date" />
 Date : <fmt:formatDate type="both" value="${now}" dateStyle="medium" timeStyle="short"/>
 

A pattern example:

<fmt:formatDate pattern="MMM dd yyyy, HH:mm:ss" value="${row[1]}"/>

In a similar fashion, it’s very easy to format a number as currency in JSTL. Example:

<fmt:formatNumber value="${row[2]}" type="currency"/> 

 One of the best books on JSTL is written by Shawn Bayern, the reference implementation lead for JSTL.
 

jstl 7:18 pm

    With JSTL it is easy to display query results on a web page. Most online tutorials list code similar to the following:

<sql:query var="queryResults" dataSource="${dataSrc}">
    select * from employees
</sql:query>
 

<c:forEach var="row" items="${queryResults.rows}">

     <c:out value="${row.id}"/>
    <c:out value="${row.lastname"/>

</c:forEach>
 

What if you want to display table values referring to the multiple array which is the result set by column index instead of column name. You should use the following syntax:

  <c:out value="${row [0]}"/>
  <c:out value="${row [1]}"/>

However, it is not enough. When you use the forEach tag, you should also use rowsByIndex property of the Result object as follows: 

<c:forEach var="row" items="${queryResults.rowsByIndex}">   

Only then your jsp will be valid and will not throw an exception.