JSP programming best practice: Use JSTL in place of Java code
JavaServer Pages Standard Tag Library (JSTL) is a collection of JSP tags that provide standard functionality most commonly sought by JSP page authors. JSTL has support for conditions, iteration, locale-sensitive formatting, and so forth. It also has an expression language (EL) that allows page authors to control how data is retrieved and displayed.
Store JSP pages should contain little or no Java code in favor of JSTL: any business logic should be delegated to page-friendly data beans, and the remaining presentation logic should be implemented in JSTL.
There are two versions of JSTL: EL (expression language) - based and RT (request-time expression) - based. For HCL Commerce, EL-based JSTL should be used.
- Core
- XML processing
- I18N (internationalization) capable formatting
- Relational database access (SQL)
As, under the HCL Commerce Programming Model, data beans constitute the only supported data access mechanism for JSP pages, the SQL library tags must not be used.
The following examples demonstrate how JSTL can be used to replace Java code.
This first example assumes that a product data bean is available to the JSP page and uses it to display the minimum item price of the product if it is available and the string Not available otherwise.
Using Java, this task can be accomplished by means of direct access to the data bean and object methods as follows:
<% if (productDataBean.getMinimumItemPrice() != null) { %>
<%= productDataBean.getMinimumItemPrice() %>
<% } else { %>
<%=storetext.getString("NO_PRICE_AVAILABLE")%>
<% } %>
Using JSTL, the same task can be accomplished by means of the
<c:out>and
<fmt:message>tags as follows:
<c:out value="${productDataBean.minimumItemPrice}"> <fmt:message key="NO_PRICE_AVAILABLE" bundle="${storeText}" /> </c:out>
(The <c:out> tag outputs the result of evaluating its value parameter or, if that is null, the value provided in its body. The <fmt:message> tag retrieves the locale-specific version of a message from a resource bundle.)
This next example assumes that a category data bean is available to the JSP page and uses it to display the name and price for each product in the category.
Using Java, this task can be accomplished by means of direct access to the data bean and object methods in conjunction with the Java control facilities as follows:
<% ProductDataBean[] products = categoryDataBean.getProducts(); for (int i=0; i < products.length; i++) { %> <%= products[i].getDescription().getName() %> <% if (products[i].getMinimumItemPrice() != null) { %> <%= products[i].getMinimumItemPrice() %> <% } else { %> <%=storetext.getString("NO_PRICE_AVAILABLE")%> <% } } %>
Using JSTL, the same task can be accomplished by means of the
<c:foreach>,
<c:out>, and
<fmt:message>tags in conjunction with the JSTL expression language facilities as follows:
<c:forEach var="product"
items="${categoryDataBean.products}">
<c:out value="${product.description.name}" />
<c:out value="${product.minimumItemPrice}">
<fmt:message key="NO_PRICE_AVAILABLE"
bundle="${storeText}" />
</c:out>
</c:forEach>
Observe that the JSTL rendition of both examples is both more compact and easier to understand.