• 5th, Aug 2010

JSTL Handbook

<!--
*
* JSP Fundamentals
*
-->
<%@ page ... %>
<!--
- Specify the syntax of a page: html, xml...
- Session Tracking
- Error page
-->
<%@ include ...%> <!-- Includes a file -->
<%@ taglib ... %> <!-- Declares a custom tag library. -->

<%jsp:useBean %>
<%jsp:getProperty ...%>
<%jsp:setProperty ...%>
<%jsp:include ...%> <!-- includes the response from a servlet or JSP -->
<%jsp:forward ...%> <!-- forwards the processing to another page -->
<%jsp:param ...%> <!-- adds a parameter value to a request -->
<%jsp:plugin ...%> <!-- browser dependent elements -->

<% code %>
<%= print %>
<%!declaration %>

<c:out value="${1+2+3}" /> <!-- Setting a bean property -->
<jsp:useBean id="msg" class="com.org..."/>

<jsp:setProperty name="msg" property="category" value"for him" />

<!-- FUNDAMENTALS -->
<!-- Output -->
<c:out value="${type}" />

<!-- Loops -->
<c:forEach var="i" begin="1" end="20" step="1" varStatus ="status">
	<c:out value="${i}" />
</c:forEach>
<c:forEach items="${types}" var="type">
	<option value="${type}"><c:out value="${type}" /></option>
</c:forEach>
<!-- Note: Do not use "param" as var name. -->

<!-- Set -->
<c:set var="m_cols" value="3"/>
<c:set var="m_cols" value="${m_cols+1}"/>

<!-- If ... Else -->
<c:choose>
<c:when test="">
...
</c:when>
<c:otherwise>
...
</c:otherwise>
</c:choose>

<!-- Dates -->
//Add library in the header
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
//Use
<fmt:formatDate value="${now}" type="both" pattern="E, dd/MM/yyyy 'at' HH:MM:ss"/>

<!-- String -->
<c:out value="${fn:substring(feed.name, 0, 50)}" />

<c:out value="${fn:substringAfter(task.class, '.task.')}" />

<c:if test="${fn:length(feed.name) > 50}"> 

<c:out value="${fn:toUpperCase('text')}" />

<!-- Get / Post Params -->
<c:if test="${! empty param.productId}">

<!-- Reporting errors, hasBindErrors -->
//Use the next library:
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
//Print all errors (1/3) simple:
<form:errors path="*"/>

//Print all errors (2/3) simple/compact:
<spring:hasBindErrors name="feedgroup">
	<div class="alert">
		<form:errors path="*" />
	</div>
</spring:hasBindErrors>

//Print all errors (3/3) detailed method:
	<spring:hasBindErrors name="feed">
		<c:if test="${errors.errorCount > 0}" >
			<div class="alert">
				Sorry, you can't submit your feed. Check the errors and please try again.<br /><br />
		         <ul>
		            <c:forEach var="errMsgObj" items="${errors.allErrors}">
		               <li>
		                  <spring:message code="${errMsgObj.code}" text="${errMsgObj.defaultMessage}"/>
		               </li>
		            </c:forEach>
		         </ul>
			</div>
		</c:if>
	</spring:hasBindErrors>

Tags: , , , , , ,

Leave a Reply

*

© 2010 unexpected[it]. All Rights Reserved.