Resources/tutorials:
- Official JavaServer Pages Standard Tag Library 1.1 Tag Reference
- Biblioteca Padrão de Tags JSP: a good introductory article with examples (PT);
- JSTL Examples
- Mixing JSTL and JSF in Web Applications
Examples:
choose
the choose tag (only one clause will be outputted):<c:choose> <c:when test="${cart.numberOfItems > 1}"> <p>Your shopping cart contains ${cart.numberOfItems} items.</p> </c:when> <c:when test="${cart.numberOfItems == 1}"> <p>Your shopping cart contains ${cart.numberOfItems} item.</p> </c:when> <c:otherwise> <p>Your shopping cart is empty.</p> </c:otherwise> </c:choose>
set
the set tag using choose to set the value (note how the var "value" is accesses trough EL in the anchor below the set tag):<c:set var="value"> <c:choose> <c:when test="${!empty selectedCategory}"> category </c:when> <c:otherwise> index.jsp </c:otherwise> </c:choose> </c:set> <a href="${value}">continue shopping</a>
url
<c:url var="url" value="chooseLanguage">
<c:param name="language" value="en"/>
</c:url>
<div class="bubble"><a href="${url}">english</a></div>
The link will have a url with a query string: "
chooseLanguage?language=en
"The url jstl tag is used in order to properly encode the request URL in the event that URL rewriting is used as a means for session tracking (when the client has cookies disabled).
fn library (JSTL functions)
See the complete fn listYou can use JSTL's functions (i.e., fn) library to perform string operations.
<%@taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %> (...) <%-- Test if servlet path contains '/confirmation'--%> <c:if test="${!fn:contains(pageContext.request.servletPath,'/confirmation')}"> </c:if>
Sem comentários:
Enviar um comentário