一、數據標籤
1.property標籤
<%
request.setAttribute("name","<font color=red>張三</font>");
%>
</head>
<body>
<s:property value="#request.name" /><br/>
<s:property value="#request.name2" default="某某人"/><br/>
<s:property value="#request.name" default="某某人" escapeHtml="false"/><br/>
當值不存在時 將使用default的值,escapeHtml可以將帶有HTML標籤的字符串轉化爲HTML輸出
2.set標籤
<s:set var="i" value="1"></s:set>
<s:property value="#i" /><br/>
<s:set var="a" value="'action範圍的值'" scope="action"></s:set>
<s:set var="p" value="'page範圍的值'" scope="page"></s:set>
<s:set var="r" value="'request範圍的值'" scope="request"></s:set>
<s:set var="s" value="'session範圍的值'" scope="session"></s:set>
<s:set var="app" value="'application範圍的值'" scope="application"></s:set>
<s:property value="#a" /><br/>
<s:property value="#attr.p"/><br/>
<s:property value="#request.r"/><br/>
<s:property value="#session.s"/><br/>
<s:property value="#application.app"/><br/>
3.Bean標籤
<s:bean name="com.java1234.model.Student" var="student">
<s:param name="name" value="'張三'"></s:param>
<s:param name="age" value="10"></s:param>
</s:bean>
<s:property value="#student.name"/>
<s:property value="#student.age"/>
4.date標籤
<%
request.setAttribute("date",new Date());
%>
<body>
${date }<br/>
當前日期:<s:date name="#request.date" format="yyyy-MM-dd"/>
</body>
5.debug標籤debug標籤主要用於輔助測試,它在頁面上生成一個超鏈接,通過該鏈接可以查看ValueStack和Stack Context 中的所有值信息。使用debug標籤只有一個id屬性,這個屬性僅僅是該元素一個引用id。 在頁面上增加<s:debug/>標籤,通過debug標籤,可以看的系統中ValueStack離得全部信息,並可以看到Stack Context中的屬性。
<s:debug></s:debug>
6.url_a標籤
<s:url action="hello" namespace="/foreground" id="h">
<s:param name="name" value="'struts2'"></s:param>
</s:url>
<s:a href="%{h}">超鏈接</s:a>
<s:a action="hello" namespace="/foreground">
<s:param name="name" value="'struts2'"></s:param>
超鏈接2
</s:a>
7.include標籤
<s:include value="head.html"></s:include>
二、控制標籤
1、ifelse標籤——條件判斷
<s:if test="#request.age<20">
年齡小於20歲
</s:if>
<s:elseif test="#request.age==20">
年齡等於20歲
</s:elseif>
<s:else>
年齡大於20歲
</s:else>
2、iterator標籤——遍歷標籤
status屬性:可選屬性,該屬性在迭代時會產生一個IteratorStatus對象,該對象可以判斷當前元素的位置,包含了以下屬性方法:
int getCount(); 迭代元素個數
int getIndex(); 迭代元素當前索引
boolean getFirst(); 是否爲第一個
boolean getEven(); 是否爲偶
boolean getLast(); 是否最後一個
bolean getOdd(); 是否爲奇 (ps:#status.odd)
<%
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>
3、append標籤——疊加
<%
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>
4、generator標籤——分隔標籤
<s:generator separator="," val="'張三,李四,王五'" var="nameList"></s:generator>
<s:iterator value="#nameList">
<s:property/>
</s:iterator>
5、Merge標籤——組合標籤(混合疊加)
<%
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>
6、Sort標籤——排序標籤
<%
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.java1234.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>
自定義排序規則 myComparator
public class MyComparator implements Comparator<Student>{
public int compare(Student s1, Student s2) {
if(s1.getAge()>s2.getAge()){
return 1;
}else if(s1.getAge()<s2.getAge()){
return -1;
}
return 0;
}
}
7、subset標籤——截取標籤
<%
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>