|
Home TOC |
|
Core Tags
The core tags include those related to expressions, flow control, and a generic way to access URL-based resources whose content can then be included and or processed within the JSP page.
Expression Tags
The
exprtag evaluates an expression and outputs the result of the evaluation to the currentJspWriterobject. It is the equivalent of the JSP syntax<%=expression%>. For example,showcart.jspdisplays the number of items in a shopping cart as follows:<c:expr value="$session:cart.numberOfItems"/>The
settag sets the value of an attribute in any of the JSP scopes (page, request, session, application). If the attribute does not already exist, it is created.The JSP scoped attribute can be set either from attribute value:
<c:set id="foo" scope="session" value="..."/><c:set id="foo"> ... </c:set>The JSTL expression language reduces the need for scripting. However, page authors will still have to deal with situations where some attributes of non-JSTL tags must be specified as expressions in the page's scripting language. The standard JSP element
jsp:useBeanis used to declare a scripting variable that can be used in a scripting language expression or scriptlet. For example,showcart.jspremoves a book from a shopping cart using a scriptlet. The ID of the book to be removed is passed as a request parameter. The value of the request parameter is first set as a page attribute (to be used later by the JSTLsql:querytag) and then declared as scripting variable and passed to thecart.removemethod:<c:set var="bookId" value="$param:Remove"/> <jsp:useBean id="bookId" type="java.lang.String" /> <% cart.remove(bookId); %> <sql:query var="books" dataSource="$bookDS"> select * from PUBLIC.books where id = ? <sql:param value="$bookId" /> </sql:query>Flow Control Tags
To execute flow control logic, a page author must generally resort to using scriptlets. For example, the following scriptlet is used to iterate through a shopping cart:
<% Iterator i = cart.getItems().iterator(); while (i.hasNext()) { ShoppingCartItem item = (ShoppingCartItem)i.next(); ... %> <tr> <td align="right" bgcolor="#ffffff"> <%=item.getQuantity()%> </td> ... <% } %>Flow control tags eliminate the need for scriptlets.
Iterator Tags
The
forEachtag allows you to iterate over a collection of objects.Here's the iteration from the previous section using the
forEachtag:<c:forEach var="item" items="$session:cart.items"> ... <tr> <td align="right" bgcolor="#ffffff"> <c:expr value="$item.quantity"/> </td> ... </c:forEach>Conditional Tags
The
iftag allows the conditional execution of its body according to value of a test attribute. The following example fromcatalog.jsptests whether the request parameterAddis not empty. If the test evaluates totrue, the page queries the database for the book record identified by the request parameter and adds the book to the shopping cart:<c:if test="$param:Add != ''"> <c:set var="bid" value="$param:Add"/> <c:declare id="bid" type="java.lang.String" /> <sql:query var="books" dataSource="$bookDS"> select * from PUBLIC.books where id = ? <sql:param value="$bid" /> </sql:query> <c:forEach var="bookRow" begin="0" items="$books.rows"> <c:declare id="bookRow" type="javax.servlet.jsp.jstl.sql.Row" /> <jsp:useBean id="addedBook" class="database.BookDetails" scope="page" /> ... <% cart.add(bid, addedBook); %> ... </c:if>The
choosetag performs conditional block execution embedded by thewhensub tags. It renders the body of the firstwhentag whose test condition evaluates to true. If none of the test conditions of nestedwhentags evaluate totrue, then the body of anotherwisetag is evaluated, if present.Import Tags
The
jsp:includeelement provides for the inclusion of static and dynamic resources in the same context as the current page. However,jsp:includecannot access resources that reside outside of the Web application and causes unnecessary buffering when the resource included is fed into another elementIn the example below, the
transformelement uses the content of the included resource as the input of its transformation. Thejsp:includeelement reads the content of the response, writes it to the body content of the enclosing transform element, which then re-reads the exact same content. It would be more efficient if thetransformelement could access the input source directly and avoid the buffering involved in the body content of the transform tag.<acme:transform> <jsp:include page="/exec/employeesList"/> <acme:transform/>The
importtag is therefore the simple, generic way to access URL based resources whose content can then be included and or processed within the JSP page. Theparamtag, analogous to thejsp:paramtag (see Param Element), can be used withimportto specify request parameters.
|
Home TOC |
|