) Use of iterator labels in struts2

Source: Internet
Author: User
Tags modulus

Before describing the use of the S: iterator label, first understand the value stack in struts2. Here we refer to the description of value stack in webwork. Because struts2 is upgraded Based on webwork, the expression of value stack in webwork is also applicable to struts2. Here we do not describe what value stack does, but there are two points to note:

A value stack is essentially a list;
Calling [N] in the stack will return a sub-stack starting from position N;
For example 2. Assuming that the value stack contains [model, action, others ],

[0] --- return [model, action, others];
[1] --- return [Action, others];
[2] --- return [others];
Now we will introduce the use of S: iterator. BelowCodeAll clips passed the test using struts2.1.6 in the development environment eclipse3.4 WTP, tomcat5.5, and jdk5.

1) access days

Defined list <string> days ["Monday", "Thursday", "Friday", "Sunday"]

View plaincopy to clipboardprint?
<S: iterator value = "days"> <s: property/> </S: iterator>
<S: iterator value = "days"> <s: property/> </S: iterator>

2) use the top keyword (filter out Monday)

defined list days ["Monday", "Thursday", "Friday", "Sunday"]

View plaincopy to clipboardprint?









Top refers to the current iteration element, which can be an object;
The top here can be replaced by [0]. Top, but [0] cannot be used. [0] indicates the entire stack object. If you call [0], the tostring () method is called to output the object information;

3) use the last/First keyword

Defined string [] [] ATS = {"1", "2", "3", "4" },{ "1", "2", "3 ", "4th "}};

View plaincopy to clipboardprint?
<! -- Traverses two-dimensional arrays. The trick here is to use 'top' as the value for the inner iterator -->
<S: iterator value = "ATS" status = "of">
<S: If test = "# Of. Last"> <br/> </S: If>
<S: iterator value = "TOP">
<! -- It can also be replaced by [0]. Top. If [0] is used, the stack object information is printed at the same time -->
<S: property/>
</S: iterator>
</S: iterator>
<! -- Traverses two-dimensional arrays. The trick here is to use 'top' as the value for the inner iterator -->
<S: iterator value = "ATS" status = "of">
<S: If test = "# Of. Last"> <br/> </S: If>
<S: iterator value = "TOP">
<! -- It can also be replaced by [0]. Top. If [0] is used, the stack object information is printed at the same time -->
<S: property/>
</S: iterator>
</S: iterator>

The status attribute in the iterator label indicates the position of the current iteration;
# Of. Last is the last element used to determine whether the current fall;
Last returns a boolean type;
First returns a boolean type;
4) use the odd/even keyword

The following example shows how each row outputs different colors.

Defined list <string> days ["Monday", "Thursday", "Friday", "Sunday"]

View plaincopy to clipboardprint?
<! -- Red for odd rows and green for even rows -->
<S: iterator value = "days" status = "offset">
<S: else>
<S: If test = "# offset. Odd = true">
<Li style = "color: Red" mce_style = "color: Red"> <s: property/> </LI>
</S: If>
<S: else>
<Li> <s: property/> </LI>
</S: else>
</S: else>
</S: iterator>
<! -- Red for odd rows and green for even rows -->
<S: iterator value = "days" status = "offset">
<S: else>
<S: If test = "# offset. Odd = true">
<Li style = "color: Red" mce_style = "color: Red"> <s: property/> </LI>
</S: If>
<S: else>
<Li> <s: property/> </LI>
</S: else>
</S: else>
</S: iterator>

The odd keyword is used to determine whether the current iteration position is an odd number of rows. Odd returns the boolean type;
The evne keyword is used to determine whether the current iteration position is an even number of rows. Boolean Type returned by even
5) In summary, when declaring the status attribute of iterator, you can use the following method through # statusname. Method:

Even: Boolean-returns true if the current iteration position is an even number.
Odd: Boolean-true if the current iteration position is an odd number
Count: int-return the count of the current iteration position (starting from 1)
Index: int-return the number of the current iteration position (starting from 0)
First: Boolean-returns true if the current iteration position is the first
Last: Boolean-returns true if the current iteration position is the last bit.
Modulus (operand: INT): int-returns the modulus of the current count (starting from 1) and the specified operand
6) Finally, let's take a look at the usage of calling value stack in iterator.

Assume that countries is a list object. Each country has a name attribute and a citys list object, and each city also has a name attribute. In this case, the following method is used to access the name attribute of countries in the iteration cities:

View plaincopy to clipboardprint?


,



,

<WW: property value = "name"/> here is ctiy. Name; <WW: property value = "[1]. Name"/> It is country. Name.
<WW: property value = "[1]. Name"/> is equivalent to <WW: property value = "[1]. Top. Name"/>
We refer to a specific position on the stack: '[1]'. the top of the stack, position 0, contains the current city, pushed on by the inner iterator; Position 1 contains the current country, pushed there by the outer iterator. (city is in the current stack, that is, top or [0], while [1] specifies the outer iterator object, that is, country)
'[N]' indicates the substack (sub-stack) with the starting position N, not just the object at the position n. Therefore, '[0]' indicates the entire stack, and '[1]' indicates all the stack elements except the top object.

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/oxcow/archive/2009/09/03/4516283.aspx

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.