Introduction to the properties of S:iterator
| Property name |
Whether you must |
Default value |
Type |
Description |
| Status |
False |
False |
String |
This property is not specified by default, and if specified, an object of the Iteratorstatus class is created, which is the value of this property and is described in more detail below |
| Value |
False |
|
String |
A collection of loops, including map, List, set, array |
| Var |
False |
|
String |
Each element in the collection, placed in the value stack, accesses the elements in the traversed collection, |
| Begin |
False |
0 |
Int |
Index to begin traversing |
| End |
|
|
|
Similar to begin |
*ineratorstatus Object Description:
Objects of this class encapsulate information about the loop, obtained by accessing its properties, and the main attributes are:
Index: The indexes of the collection that are currently looping to
Count: Number of cycles already
First: Whether it is a cycle
Last: Whether it is the final loop
Odd: Whether the current position is an odd number
Even: whether the current position is an even number
eg
Create a new class Action1.java with the following code:
Packagemy.test;ImportCom.opensymphony.xwork2.ActionSupport; Public classAction1extendsactionsupport{PrivateString[] Arr=NewString[5]; //Be sure to add the Get/set method, or a null pointer exception will appear Publicstring[] Getarr () {returnarr; } Public voidSetarr (string[] arr) { This. arr =arr; } PublicString Execute () {intI=101; //The assignment for the following for loop does not work for(String A:arr) {a= "" +i++; } arr[3]= "This is 3";//this verifies that the above for loop assignment to Arr is not working, which is determined by the character of the string array and string, and the results of the operation can be returnSUCCESS; }}
New JSP file/iterator.jsp, remember to introduce struts tags
<%@ Page Language="Java"ContentType="text/html; Charset=utf-8"pageencoding="UTF-8"%><%@ taglib Prefix="s"URI="/struts-tags"%><!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd "><HTML><Head><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8"><title>Insert Title here</title></Head><Body> <S:debug></S:debug> <S:iteratorvar= "One"value= "Arr"begin= "2"Status= "STA"> <!--because the loop-to-value is saved to the value stack, the following values are obtained through the s:property tag -the values to loop to are:<S:propertyvalue= "One"/><BR>the number of cycles is:<S:propertyvalue= "#sta. Count"/><BR>the index of the loop is:<S:propertyvalue= "#sta. Index"/><BR>is an odd number:<S:propertyvalue= "#sta. Odd"/><HR> </S:iterator></Body></HTML>
Last configuration file to one:
<?XML version= "1.0" encoding= "UTF-8"?><!DOCTYPE struts Public "-//apache software foundation//dtd struts Configuration 2.3//en" "Http://struts.apache.or G/dtds/struts-2.3.dtd "><Struts> <constantname= "Struts.enable.DynamicMethodInvocation"value= "false" /> <constantname= "Struts.devmode"value= "true" /> < Packagename= "Test"namespace="/"extends= "Struts-default"> <Actionname= "iterator"class= "My.test.Action1"> <result>/iterator.jsp</result> </Action> <Actionname= "Index"> <resulttype= "Redirectaction"> <paramname= "ActionName">HelloWorld</param> <paramname= "namespace">/example</param> </result> </Action> </ Package> <!--ADD Packages here -</Struts>
The author uses the STRUTS2 2.3.30 version, different versions of the configuration file are not the same drip
Run One:
The above debug if in doubt, self-baidu ~
S:iterator Usage and examples