关于静态网站开发相关新闻,网站建设需求策划书,百度手机助手官网,工作服定制厂家1.在说明s:iterator标签的使用前#xff0c;先了解下struts2中的Value Stack。 这里参考了webwork中对Value Stack的描述#xff0c;由于struts2是在webwork的基础上进行升级的#xff0c; 因此webwork对于Value Stack的表述同样适用于struts2。在这里不描述Value Stack具… 1.在说明s:iterator标签的使用前先了解下struts2中的Value Stack。 这里参考了webwork中对Value Stack的描述由于struts2是在webwork的基础上进行升级的 因此webwork对于Value Stack的表述同样适用于struts2。在这里不描述Value Stack具体做什么但有两点需要注意
2. 一个value stack本质上是一个List
3.在栈中调用[n]将返回一个从位置n开始的子栈
4.对于2举个例子说明。假定Value Stack包含了[model,action,others]那么
5.
6.[0] --- 返回 [model,action,others]
7.[1] --- 返回 [action,others]
8.[2] --- 返回 [others];
9.现在将开始介绍s:iterator的一些使用。以下代码片段均在开发环境eclipse3.4 wtp、tomcat5.5、jdk5上使用struts2.1.6测试通过。
10.
11.1) 、访问 days
12.
13.defined ListString days [Monday,Thursday,Friday,Sunday]
14.
15.view plaincopy to clipboardprint?
16.s:iterator valuedayss:property //s:iterator
17.s:iterator valuedayss:property //s:iterator
18.
19.2) 、使用 top 关键字使用(过滤掉Monday)
20.
21.defined ListString days [Monday,Thursday,Friday,Sunday]
22.
23.view plaincopy to clipboardprint?
24.s:iterator valuedays
25. s:if testtop!Monday
26. s:property /
27. /s:if
28./s:iterator
29.s:iterator valuedays
30. s:if testtop!Monday
31. s:property /
32. /s:if
33. /s:iterator
34.
35.top 指代当前迭代元素可以为对象
36.这里的top可用[0].top替代但不能使用[0]。 [0]代表整个栈对象。如果单纯调用[0]将会调用其toString()方法输出对象信息
37.
38.3)、使用 last / first 关键字使用
39.
40.defined String[][] aTs { { 一, 二, 三, 四 },{ 一一, 二二, 三三, 四四} };
41.
42.view plaincopy to clipboardprint?
43.!--遍历二维数组The trick here is to use top as the value for the inner iterator--
44. s:iterator valueaTs statusof
45. s:if test#of.lastbr//s:if
46. s:iterator valuetop
47.!--亦可用[0].top替代。如果单纯用[0],则会同时打印该处栈对象信息--
48. s:property /
49. /s:iterator
50. /s:iterator
51.!--遍历二维数组The trick here is to use top as the value for the inner iterator--
52. s:iterator valueaTs statusof
53. s:if test#of.lastbr//s:if
54. s:iterator valuetop
55.!--亦可用[0].top替代。如果单纯用[0],则会同时打印该处栈对象信息--
56. s:property /
57. /s:iterator
58. /s:iterator
59.
60.
61.iterator 标签中的status属性代表当前迭代的位置
62.#of.last用于判断当前是否跌到的最后一个元素
63.last返回一个boolean类型;
64.first 返回一个boolean类型;
65.4)、使用 odd / even 关键字
66.
67.下面的例子要实现每行输出颜色不同的效果。
68.
69.defined ListString days [Monday,Thursday,Friday,Sunday]
70.
71.view plaincopy to clipboardprint?
72.!--奇数行显示为红色,偶数行显示为绿色--
73. s:iterator valuedays statusoffset
74. s:else
75. s:if test#offset.oddtrue
76. li stylecolor: red mce_stylecolor: reds:property //li
77. /s:if
78. s:else
79. lis:property //li
80. /s:else
81. /s:else
82./s:iterator
83. !--奇数行显示为红色,偶数行显示为绿色--
84. s:iterator valuedays statusoffset
85. s:else
86. s:if test#offset.oddtrue
87. li stylecolor: red mce_stylecolor: reds:property //li
88. /s:if
89. s:else
90. lis:property //li
91. /s:else
92. /s:else
93. /s:iterator
94.
95.odd关键字用来判断当前迭代位置是否为奇数行。odd返回boolean类型;
96.evne关键字用来判断当前迭代位置是否为偶数行。even返回boolean类型
97.5)、总结下当声明iterator的status属性时通过#statusName.method 可以使用以下方法:
98.
99.even : boolean - 如果当前迭代位置是偶数返回true
100.odd : boolean - 如果当前迭代位置是奇数返回true
101.count : int - 返回当前迭代位置的计数(从1开始)
102.index : int - 返回当前迭代位置的编号(从0开始)
103.first : boolean - 如果当前迭代位置是第一位时返回true
104.last : boolean - 如果当前迭代位置是最后一位时返回true
105.modulus(operand : int) : int - 返回当前计数(从1开始)与指定操作数的模数
106.6)、最后再来看下在iterator中调用value stack的用法。
107.
108.假定countries是一个List对象每一个country有一个name属性和一个citys List对象并且每一个city也有一个name属性 。那么我们想要在迭代cities是访问countries的name属性就的用如下方式
109.
110.
111.view plaincopy to clipboardprint?
112.s:iterator valuecountries
113. s:iterator valuecities
114. s:property valuename/, s:property value[1].name/br
115. /s:iterator
116./s:iterator
117. s:iterator valuecountries
118. s:iterator valuecities
119. s:property valuename/, s:property value[1].name/br
120. /s:iterator
121. /s:iterator
122.
123.这里的 ww:property valuename/取的是ctiy.name;ww:property value[1].name/取得是country.name
124.ww:property value[1].name/ 等价于 ww:property value[1].top.name/
125.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处于当前栈即top或者[0],而[1]指明了外层iterator对象即country)
126. [n]标记引用开始位置为n的子栈sub-stack而不仅仅是位置n处的对象。 因此[0]代表整个栈而[1]是除top对象外所有的栈元素。 转载于:https://www.cnblogs.com/huidaoli/p/3255366.html