1. 程式人生 > >第 24講 struts2標籤-控制標籤

第 24講 struts2標籤-控制標籤

Struts2 控制標籤接上一節Ifelse 標籤:條件判斷標籤;在request設定值, <%@ page language="java" contentType="text/html; charset=UTF-8"     pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd"> <%@taglib prefix="s" uri="/struts-tags" %> <html> <head> <meta http-equiv="Content-Type"

 content="text/html; charset=UTF-8"> <title>Insert title here</title> <%     int age=2;     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> </html>Iterator 標籤:遍歷標籤;
<%@ page language="java" contentType="text/html; charset=UTF-8"     pageEncoding="UTF-8"%> <%@ page import="com.cruise.model.Student" %> <%@ page import="java.util.*" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd"> <%@taglib prefix="s"
 uri="/struts-tags" %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> <%     List<Student> studentList=new ArrayList<Student>();     studentList.add(new Student(1,"張三",10));     studentList.add(new Student(3,"李四",20));     studentList.add(new Student(5,"王五",30));     request.setAttribute("studentList",studentList); %> </head> <body> <table>     <tr>        <th>序號</th>        <th>編號</th>        <th>姓名</th>        <th>年齡</th>     </tr>     <s:iterator value="#request.studentList" status="status">     <tr>        <td><s:property value="#status.index+1"/></td>        <td><s:property value="id"/></td>        <td><s:property value="name"/></td>        <td><s:property value="age"/></td>     </tr>     </s:iterator> </table> </body> </html>Append 標籤:疊加標籤;將兩個List放在一起,形成一個List,前提是兩個List集合的物件有相同的屬性,合併之後才能在一起遍歷 <%@ page language="java" contentType="text/html; charset=UTF-8"     pageEncoding="UTF-8"%> <%@ page import="com.cruise.model.Student" %> <%@ page import="java.util.*" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd"> <%@taglib prefix="s" uri="/struts-tags" %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> <%     List<Student> studentList1=new ArrayList<Student>();     List<Student> studentList2=new ArrayList<Student>();     studentList1.add(new Student(1,"張三",10));     studentList1.add(new Student(3,"李四",20));     studentList2.add(new Student(5,"王五",30));     studentList2.add(new Student(7,"趙六",40));     request.setAttribute("studentList1",studentList1);     request.setAttribute("studentList2",studentList2); %> </head> <body> <s:append var="studentList3">     <s:param value="#request.studentList1"></s:param>     <s:param value="#request.studentList2"></s:param> </s:append> <table>     <tr>        <th>序號</th>        <th>編號</th>        <th>姓名</th>        <th>年齡</th>     </tr>     <s:iterator value="studentList3" status="status">     <tr>        <td><s:property value="#status.index+1"/></td>        <td><s:property value="id"/></td>        <td><s:property value="name"/></td>        <td><s:property value="age"/></td>     </tr>     </s:iterator> </table> </body> </html>Generator 標籤:分隔標籤;字串的分割,和split()方法一樣 <%@ page language="java" contentType="text/html; charset=UTF-8"     pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd"> <%@taglib prefix="s" uri="/struts-tags" %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <s:generator separator="," val="'張三,李四,王五'" var="nameList"></s:generator> <s:iterator value="#nameList">     <s:property/> </s:iterator></table> </body> </html>Merge 標籤:組合標籤;將兩個List集合混合,順序發生了變化。 <%@ page language="java" contentType="text/html; charset=UTF-8"     pageEncoding="UTF-8"%> <%@ page import="com.cruise.model.Student" %> <%@ page import="java.util.*" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd"> <%@taglib prefix="s" uri="/struts-tags" %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> <%     List<Student> studentList1=new ArrayList<Student>();     List<Student> studentList2=new ArrayList<Student>();     studentList1.add(new Student(1,"張三",10));     studentList1.add(new Student(3,"李四",20));     studentList2.add(new Student(5,"王五",30));     studentList2.add(new Student(7,"趙六",40));     request.setAttribute("studentList1",studentList1);     request.setAttribute("studentList2",studentList2); %> </head> <body> <s:merge var="studentList3">     <s:param value="#request.studentList1"></s:param>     <s:param value="#request.studentList2"></s:param> </s:merge> <table>     <tr>        <th>序號</th>        <th>編號</th>        <th>姓名</th>        <th>年齡</th>     </tr>     <s:iterator value="studentList3" status="status">     <tr>        <td><s:property value="#status.index+1"/></td>        <td><s:property value="id"/></td>        <td><s:property value="name"/></td>        <td><s:property value="age"/></td>     </tr>     </s:iterator> </table> </body> </html>Sort 標籤:排序標籤;需要引入一個比較器, <%@ page language="java" contentType="text/html; charset=UTF-8"     pageEncoding="UTF-8"%> <%@ page import="com.cruise.model.Student" %> <%@ page import="java.util.*" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd"> <%@taglib prefix="s" uri="/struts-tags" %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> <%     List<Student> studentList1=new ArrayList<Student>();     studentList1.add(new Student(1,"張三",20));     studentList1.add(new Student(3,"李四",10));     studentList1.add(new Student(5,"王五",40));     studentList1.add(new Student(7,"趙六",30));     request.setAttribute("studentList1",studentList1); %> </head> <body> <s:bean id="myComparator" name="com.cruise.comparator.MyComparator"></s:bean> <table>     <tr>        <th>序號</th>        <th>編號</th>        <th>姓名</th>        <th>年齡</th>     </tr>     <s:sort comparator="#myComparator" source="#request.studentList1" >     <s:iterator status="status">     <tr>        <td><s:property value="#status.index+1"/></td>        <td><s:property value="id"/></td>        <td><s:property value="name"/></td>        <td><s:property value="age"/></td>     </tr>     </s:iterator>     </s:sort> </table> </body> </html>Subset 標籤:擷取標籤,類似與substring()方法,獲取List集合中的資料, <%@ page language="java" contentType="text/html; charset=UTF-8"     pageEncoding="UTF-8"%> <%@ page import="com.cruise.model.Student" %> <%@ page import="java.util.*" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd"> <%@taglib prefix="s" uri="/struts-tags" %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> <%     List<Student> studentList1=new ArrayList<Student>();     studentList1.add(new Student(1,"張三",20));     studentList1.add(new Student(3,"李四",10));     studentList1.add(new Student(5,"王五",40));     studentList1.add(new Student(7,"趙六",30));     request.setAttribute("studentList1",studentList1); %> </head> <body> <table>     <tr>        <th>序號</th>        <th>編號</th>        <th>姓名</th>        <th>年齡</th>     </tr>     <s:subset source="#request.studentList1" count="2" start="2">     <s:iterator status="status">     <tr>        <td><s:property value="#status.index+1"/></td>        <td><s:property value="id"/></td>        <td><s:property value="name"/></td>        <td><s:property value="age"/></td>     </tr>     </s:iterator>     </s:subset> </table> </body> </html>