EQUIP2 WebApp Dynamic Pages with JSP

Chris Greenhalgh, 2007-01-30

Introduction

This document gives some general pointers and suggestions on use of JSP for dynamic HTML generation (no EQUIP2).

See EQUIP2_WebApp_Dynamic_Pages_using_EQUIP2_Taglib.html for EQUIP2-specific JSP information.

JSP options

Tomcat 5.5 supports Servlet 2.4 (JSR 154) & JSP 2.0 (JSR 152).

JSP 2.0 supports:

Actions available include:

As with normal Java servlets/JSPs the four scopes of objects available to pages are:

JSP Spring custom tags

See Spring docs/taglib for details. The taglib is introduced using the page directive:
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
The most immediately useful tag is
<spring:bind path="BEAN.PROPERTY"> ... ${status.value} ... </spring:bind>
which binds the value of the named property of the named (Spring/request scope) bean to the page variable status within the scope of the tag. These are useful with Spring's own form support, to guide its population of backing objects. See EQUIP2_WebApp_Form_Handling.html
 and simple_form_controller_form.jsp for more information and an example of use.

JSTL standard tags & JSP EL

JSTL standard tags and JSP expression language provide a good way to access information and do XSLT-like programming in JSPs. The taglibs are introduced into (each) JSP using the page directives:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>

Refer to the JSTL 1.1 specification (or similar) for complete documentation.

Examples

Some example JSPs are present in WEB-INF/jsp/simplerequest/view/...

include.jsp

use of jsp:include tag:

<jsp:include page="included.jsp">
<jsp:param name="parameter" value="foo"/>
</jsp:include>

scriptlet.jsp

scriptlets (tag are preferred!). E.g.:

<%= 3+4 %>
<%= new java.util.Random().nextInt() %>
<% int x=4; %>
<%= x %>
<% for (int i=0; i<10; i++) { %>
<%-- ... --%>
<% } %>

jstl.jsp

Java Standard Template Library (preferred). E.g.
<%-- declare taglibs --%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>

<%-- unlike XSLT there are mutable variables --%>
<c:set var="x" value="6"/>
<c:out value="${x}"/>

<%-- simple condition --%>
<c:if test="${x==6}">
<%-- ... --%>
</c:if>

<%-- multi-case condition c.f. switch --%>
<c:choose>
<c:when test="${x==5}">Aha!</c:when>
<c:otherwise>No aha...</c:otherwise>
</c:choose>

<%-- simple loop (use c:forItems to enumerate list/array --%>
<c:set var="names" value="a,b,c,d,e"/>
<c:forTokens var="name" items="${names}" delims=",">
<p>name = <c:out value="${name}"/></p>
</c:forTokens>

Note that values in the 'model' returned from a Spring controller are passed in the request, which is accessed from JSP EL as (e.g. the 'arg' property of the object called 'command' in the returned model):

<c:out value="${requestScope.command.arg}"/>

Changes

2007-01-30