Develop > Presentation layer > Work with JSP pages > WebSphere Commerce JSP programming best practices


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 WebSphere Commerce, EL-based JSTL should be used.

JSTL 1.0 consists of four functional areas, which are exposed as separate tag libraries with their own namespaces:

As, under the WebSphere 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.

Use Java, this task can be accomplished by means of direct access to the data bean and object methods...

<% if (productDataBean.getMinimumItemPrice() != null) { %>    
<%= productDataBean.getMinimumItemPrice() %>
<% } else { %>    
<%=storetext.getString("NO_PRICE_AVAILABLE")%>
<% } %>

Use 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.

Use 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")%>
<% } 
}
%>

Use 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...

<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.


Related concepts

Model-View-Controller design pattern

Display design pattern

WebSphere Commerce framework overview

Related reference

WebSphere Commerce JSP programming best practices


+

Search Tips   |   Advanced Search