struts2控制标签:
条件标签:if...elseif...else
主要属性:test表示测试属性。
<% int age=11; request.setAttribute("age", age); %> </head> <body> <s:if test="#request.age<20"> 年龄小于20岁。 </s:if> <s:elseif test="#request.age==20"> 年龄等于20岁 </s:elseif> <s:else> 年龄大于20岁 </s:else> </body>
迭代标签iterator:
用于循环数组,集合和Map。
属性:value表示:数组,集合或map。Var表示当前元素。Status表示当前元素的状态。
1 <% 2 List<Student> studentList=new ArrayList<Student>(); 3 studentList.add(new Student(1,"张三",10)); 4 studentList.add(new Student(3,"李四",20)); 5 studentList.add(new Student(6,"王五",30)); 6 7 request.setAttribute("studentList", studentList); 8 %> 9 </head> 10 <body> 11 <table> 12 <tr> 13 <th>序号</th> 14 <th>编号</th> 15 <th>姓名</th> 16 <th>年龄</th> 17 </tr> 18 <s:iterator value="#request.studentList" status="status"> 19 <tr> 20 <td><s:property value="#status.index+1"/></td> 21 <td><s:property value="id"/></td> 22 <td><s:property value="name"/></td> 23 <td><s:property value="age"/></td> 24 </tr> 25 </s:iterator> 26 </table> 27 </body>
集合合并标签append和merge:
将多个集合合并成成一个集合。
属性:var表示新集合的名称。
1 <% 2 List<Student> studentList1=new ArrayList<Student>(); 3 List<Student> studentList2=new ArrayList<Student>(); 4 studentList1.add(new Student(1,"zhangsan",20)); 5 studentList1.add(new Student(3,"lisi",30)); 6 studentList2.add(new Student(5,"wangwu",40)); 7 studentList2.add(new Student(7,"zhaoliu",50)); 8 9 request.setAttribute("studentList1", studentList1); 10 request.setAttribute("studentList2", studentList2); 11 %> 12 </head> 13 <body> 14 <s:merge var="studentList3"> 15 <s:param value="#request.studentList1"></s:param> 16 <s:param value="#request.studentList2"></s:param> 17 </s:merge> 18 19 <table> 20 <tr> 21 <th>序号</th> 22 <th>编号</th> 23 <th>姓名</th> 24 <th>年龄</th> 25 </tr> 26 <s:iterator value="studentList3" status="status"> 27 <tr> 28 <td><s:property value="#status.index+1"/></td> 29 <td><s:property value="id"/></td> 30 <td><s:property value="name"/></td> 31 <td><s:property value="age"/></td> 32 </tr> 33 </s:iterator> 34 </table> 35 </body>
1 <% 2 List<Student> studentList1=new ArrayList<Student>(); 3 List<Student> studentList2=new ArrayList<Student>(); 4 5 studentList1.add(new Student(1,"fangchao1",10)); 6 studentList1.add(new Student(2,"fangchao2",20)); 7 studentList2.add(new Student(3,"fangchao3",30)); 8 studentList2.add(new Student(4,"fangchao4",40)); 9 10 request.setAttribute("studentList1", studentList1); 11 request.setAttribute("studentList2", studentList2); 12 %> 13 </head> 14 <body> 15 <s:append var="studentList3"> 16 <s:param value="#request.studentList1"></s:param> 17 <s:param value="#request.studentList2"></s:param> 18 </s:append> 19 20 <table> 21 <tr> 22 <th>序号</th> 23 <th>编号</th> 24 <th>姓名</th> 25 <th>年龄</th> 26 </tr> 27 <s:iterator value="studentList3" status="status"> 28 <tr> 29 <td><s:property value="#status.index+1"/> </td> 30 <td><s:property value="id"/></td> 31 <td><s:property value="name"/></td> 32 <td><s:property value="age"/></td> 33 </tr> 34 35 </s:iterator> 36 37 </table> 38 </body>
字符串分割标签generator:
将一个字符串按照指定分隔符分割成一个字符串数组。
属性:separator表示分隔符。value表示字符串。count表示字符串数组中元素个数
1 <s:generator separator="," val="张三,李四,王五" var="nameList"> 2 </s:generator> 3 4 <s:iterator value="#nameList"> 5 <s:property/> 6 </s:iterator>
集合子集标签subset:
从集合中取出一个子集。
属性:source表示集合。start表示起始索引。count表示子集中元素的个数。
1 <% 2 List<Student> studentList1=new ArrayList<Student>(); 3 4 5 studentList1.add(new Student(1,"fangchao1",10)); 6 studentList1.add(new Student(2,"fangchao2",20)); 7 studentList1.add(new Student(3,"fangchao3",30)); 8 studentList1.add(new Student(4,"fangchao4",40)); 9 10 request.setAttribute("studentList1", studentList1); 11 12 %> 13 </head> 14 <body> 15 <table> 16 <tr> 17 <th>序号</th> 18 <th>编号</th> 19 <th>姓名</th> 20 <th>年龄</th> 21 </tr> 22 <s:subset source="#request.studentList1" count="2" start="2"> 23 <s:iterator value="studentList3" status="status"> 24 <tr> 25 <td><s:property value="#status.index+1"/> </td> 26 <td><s:property value="id"/></td> 27 <td><s:property value="name"/></td> 28 <td><s:property value="age"/></td> 29 </tr> 30 </s:iterator> 31 </s:subset> 32 </table> 33 </body>
排序标签sort
根据定义的排序规则,对集合中的元素进行排序,排序后生成的新的集合值栈顶部,标签结束后自动删除。
属性:comparator表示Comparator的类。source表示集合。
1 <% 2 List<Student> studentList1=new ArrayList<Student>(); 3 4 5 studentList1.add(new Student(1,"fangchao1",10)); 6 studentList1.add(new Student(2,"fangchao2",20)); 7 studentList1.add(new Student(3,"fangchao3",30)); 8 studentList1.add(new Student(4,"fangchao4",40)); 9 10 request.setAttribute("studentList1", studentList1); 11 12 %> 13 </head> 14 <body> 15 <s:bean name="com.fangchao.comparator.MyComparator" id="myComparator"></s:bean> 16 17 <table> 18 <tr> 19 <th>序号</th> 20 <th>编号</th> 21 <th>姓名</th> 22 <th>年龄</th> 23 </tr> 24 <s:sort comparator="#myComparator" source="#request.studentList1"> 25 <s:iterator status="status"> 26 <tr> 27 <td><s:property value="#status.index+1"/> </td> 28 <td><s:property value="id"/></td> 29 <td><s:property value="name"/></td> 30 <td><s:property value="age"/></td> 31 </tr> 32 33 </s:iterator> 34 </s:sort> 35 </table> 36 </body>