Wednesday, October 17, 2012

JSTL

  • JSP without JSTL
<UL>
<% for(int i=0; i<messages.length; i++) {
String message = messages[i];
%>
    <LI><%= message %>
<% } %>
</UL>
  • JSP with JSTL
<UL>
<c:forEach var="message" items="${messages}">
    <LI>${message}
</c:forEach>
</UL>

<c:out value="${foo}" default="${calculatedValue}"/>

<c:if test="${someTest}">
    Content
</c:if>

<c:choose>
    <c:when test="test1">Content1</c:when>
    <c:when test="test2">Content2</c:when>
    ...
    <c:when test="testN">ContentN</c:when>
    <c:otherwise>Default Content</c:otherwise>
</c:choose>

<c:import>
  • Read content from arbitrary URLs
  1. Insert into page
  2. Store in variable
  3. Or make accessible via a reader
  • Unlike <jsp:include>, not restricted to own system
<c:redirect> Redirects response to specified URL
<c:param>
– Encodes a request parameter and adds it to a URL
– May be used within body of <c:import> or <c:redirect>

<fmt:formatNumber>  Formats a numeric value as a number, currency value, or
percent, in a locale-specific manner
<fmt:parseNumber> Reads string representations of number, currency value, or percent
<fmt:formatDate>
<fmt:parseDate>
<fmt:timeZone>
<fmt:setTimeZone>

Can build your own custom tags based on JSTL tags.

No comments:

Post a Comment