Jstl's Iteration Tag library
The full name of Jstl is the Java Server Pages standard tag Library, which translates to the JSP standards tag libraries, which contain a set of standard tags that are often used when developing JSP pages. These tags provide a way to develop complex JSP pages without embedding Java code.
In Jstl, there are 5 categories of tag libraries, which have an important core tag library, and the core tag library is divided into common tag library, condition tag Library, iteration tag library according to the function, and today we are going to say is the iteration tag library.
In the development of JSP, we often need to iterate over the collection object. For example, the list shows query results, and so on, traditionally using Java code to implement collection object traversal, such as List,iterator. Now we're talking about a jstl iteration tag that simplifies iterative operations to a great extent.
The <c:forEach> tag has two syntax formats, one for iterating over the members of the collection object, and another for making the statement loop through the specified number of times, which we'll enumerate.
1. Iterating through the members of a collection object
< c:foreach var items = "CollectionName" Varstatus = " Varstatuwsname " begin end = "EndIndex" step = "step" > display content </ c:foreach >
Above is the basic syntax format for a foreach tag, where the Var attribute is the member that represents the current traversal, items refers to the collection object being iterated, and the Varstatus property is used to hold information about the member that the Var refers to, such as an index, where begin indicates the starting position, default is 0, End indicates the step of the loop, which defaults to 1. Well, now that we know what his basic grammar and attributes are, what is it like to bring him into the program? Here is a simple demo for everyone to a visual presentation:
<%List<String>List= NewArrayList<String>(); List.add ("Xiao Ming"); List.add ("Little Red"); List.add ("Xiao Gang"); Request.setattribute ("List", list); %> <C:foreachvar= "VarName"Items= "${list}"Varstatus= "Varstatusname">${varname}</C:foreach>
This enables a simple iterative effect, in which the general Jstl tag is associated with the El expression so that the data can be easily obtained and exported. As you can see, it is simple to traverse the loop object directly, so we continue to look at his next syntax format, which is the number of executions of the specified statement.
2. Specify the number of executions of the statement:
<var= "VarName" varstatus= "Varstatuwsname" Begin= "Beginindex" end= "EndIndex" step= "Step"> display content </c:foreach>
Does it look familiar, his grammar is not much different from the above, but the difference is that format 2 is not a traversal of a collection object, but rather the number of times the body content is fixed by the set begin property, the End property, and the Step property. This method in the actual development is very extensive, in many development to display the list, the report, below we use a person list interlaced color of the core code to demonstrate his effect:
<Tablestyle= "border:1px solid blue; width:400px;"> <TRstyle= "Font-weight:bold; "> <TD>Name</TD><TD>Position</TD><TD>Age</TD> </TR> <!--A collection has been added to the servlet page - <C:foreachvar= "VarName"Items= "${list}"Varstatus= "status"> <TR<c:if Test= "${status.index%2==0}">Style= "Background-color:pink"</c:if></TR> <TD>${varname.name}</TD><TD>${varname.job}</TD><TD>${varname.age}</TD> </TR> </C:foreach> </Table>
This gives you a nice, interlaced effect:
Is it very useful? If you want to use jquery or HTML code to implement this style of connection with the database is very cumbersome, using the JSTL tag to make the code easier, and the conditional tags can make the code more changes.
Dark Horse Programmer "JSTL's Iteration Tag library"