Core JSTL Mastering the JSPT Standard Tag Library [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Core JSTL Mastering the JSPT Standard Tag Library [Electronic resources] - نسخه متنی

David M. Geary

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید











Chapter 4. Iteration Actions


Topics in This Chapter


  • The <c:forEach> Action


  • The <c:forTokens> Action


  • Iteration Status


  • Custom Iteration Actions



In any programming language, the ability to iterate over a set of values is essential. Before JSTL, JSP did not provide an explicit means to accomplish that fundamental task, so developers had two choices: use a scriptlet or implement a custom action, neither of which is very attractive.

JSTL provides two actions that iterate over a set of values: <c:forEach> and <c:forTokens>. JSTL also exposes an interface and two classes that you can use to implement custom iteration actions: LoopTag, LoopTagSupport, and LoopTagStatus, respectively.[1] This chapter describes the <c:forEach> and <c:forTokens> actions and shows you how to implement custom actions using the LoopTag interface and the LoopTagSupport and LoopStatus classes.

[1] LoopTag, LoopTagSupport, and LoopTagStatus all reside in the javax.servlet.jsp.jstl.core directory.


To iterate over a set of values with a scriptlet, you must be proficient in the Java programming language and you must also understand how Java and HTML interact within JSP pages. For example, consider the following code fragment:


<% int[] values = (int[])request.getAttribute("primitiveValues");
for(int i=0; i < values.length; ++i) { %>
value = <%= values[i] %><br>
<% } %>

The preceding scriptlet iterates over an array of ints stored in request scope. That scriptlet is short, but it requires you to be familiar with the following constructs:


  • Casting: (int[])request.getAttribute(...)


  • for loop syntax: for(;;)


  • Array length property: values.length


  • Accessing array values: values[i]


  • JSP Expression Syntax: <%= ... %>


  • Mixing Java and HTML: <% int[]... { %> ... <% } %>



Even for seasoned Java developers, it's not uncommon to forget the required cast in the preceding code fragment. If you are a page author and you're not familiar with the constructs listed above, or if you're a veteran Java developer and you want to avoid scriptlets, the JSTL iteration actions are for you. Consider the simplicity of the following code fragment, which is functionally identical to the preceding scriptlet:


<c:forEach items='${primitiveValues}' var='item'>
value = <c:out value='${item}'/><br>
</c:forEach>

The preceding code fragment uses the Iterating Over Data Structures" on page 158 for more information about iterating over maps with <c:forEach>.


Figure 4-1 shows a JSP page that contains both of the preceding code fragments.

Figure 4-1. Looping Over Primitive Types with a Scriptlet vs. Looping with JSTL


The JSP page shown in Common Mistakes" on page 95 for more information about missing taglib declarations.


Listing 4.1 Looping with a Scriptlet vs. Using JSTL


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Looping Over Primitive Values</title>
</head>
<body>
<%-- The taglib directive for the JSTL core actions --%>
<%@ taglib uri='http://java.sun.com/jstl/core' prefix='c' %>
<%-- Create an array of ints and store them in request
scope --%>
<% int[] primitiveValues = {1,2,3,4,5,6,7,8,9,10};
pageContext.setAttribute("primitiveValues",
primitiveValues,
PageContext.REQUEST_SCOPE);
%>
<%-- Loop over the array and print its values using
a scriptlet --%>
Looping Over Arrays of Primitive Types With a Scriptlet:<p>
<%
int[] values = (int[])request.getAttribute(
"primitiveValues");
for(int i=0; i < values.length; ++i) { %>
value = <%= values[i] %><br>
<% }
%>
<p>
<%-- Loop over the array and print its values using
JSTL's <c:forEach> action --%>
Looping Over Arrays of Primitive Types with JSTL:<p>
<c:forEach items='${primitiveValues}' var='item'>
value = <c:out value='${item}'/><br>
</c:forEach>
</body>
</html>



    / 124