struts2 标签的使用之二 s:iterator
struts2的s:iterator 可以遍历 数据栈里面的任何数组,集合等等 以下几个简单的demo:
s:iterator 标签有3个属性:
??? value:被迭代的集合
??? id?? :指定集合里面的元素的id
??? status 迭代元素的索引
1:jsp页面定义元素写法 数组或list
4:遍历2个list;
List<AttrName> attrN {color,size,style}
List<AttrValue> attrV {red,20,gay}
这2个list的元素是一一对应的,一个attrN对应一个attrV
??? <option value="<s:property value='#cid'/>"<s:if test="%{#cid==#request.address.state}">selected="selected" </s:if>><s:property value="#cid" /></option>
</s:iterator>
s:iterator value="%{au_states}" id="cid"也可以写成s:iterator value="au_states" id="cid"
在遍历内部的判断拿HttpRequest中的state何该比较。
当然如果是其他对象完全可以写成<s:if test="#cid.recommendable == 1"> 或者调用方法<s:if test="#cid.recommendable.equals('1')"> 都可以实现
6:遍历标签从request拿数据
<s:set name="index" value="0" scope="request"/>
??<s:iterator value="#request.recommend_catalogList" id="cid2" status="st2">
???<s:if test="#cid2.recommendable != 1">
???<s:set name="index" value="#request['index'] + 1" scope="request"/>
????? <li><a href="<%if("0".equals(pageType)){%>/requestnow/<s:property value="#cid2.getCanonicalPath()"/>?catalogSn=<s:property value="#cid2.categorySn"/><%}else {%><%=pathPrefix %><s:property value="#cid2.categorySn"/><%} %>"><s:property value='#cid2.name'/></a></li>
???</s:if>
???<s:if test="#request['index'] != 0 && #request['index'] % 3 == 0"><li /></s:if>
??</s:iterator>
??<s:if test="#request['index'] % 3 != 0"><li /></s:if>
当然判断里可以写成任何的表达式
????? <s:if test="%{#st.index%4 == 0}">
???? ????<s:if test="%{#st.index >=40 }">
???? ?????<tr rel="buyer_detail" class="hidden">
???? ????</s:if>
???? ????<s:else>
???? ?????<tr rel="buyer_detail">
???? ????</s:else>
???? ???</s:if>