引入標籤
<%@taglib prefix="s" uri="/struts-tags"%>
一、通用標籤(Generic Tag)
1、數據標籤
- action配置
<action name="Data_label" class="com.pb06.demo01.Tags">
<result>/jsp/06_Struts2標籤/01_數據標籤.jsp</result>
</action>
- action類代碼
public class Tags extends ActionSupport {
private Date currentDate;
private String name;
@Override
public String execute() throws Exception {
currentDate = new Date();
name = "王五";
return SUCCESS;
}
public Date getCurrentDate() {
return currentDate;
}
public void setCurrentDate(Date currentDate) {
this.currentDate = currentDate;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
- 頁面代碼(重點)
<body>
<!-- 輸出值棧和Stack Context -->
<!-- value:用來獲取值的OGNL表達式
default:value屬性爲空時輸出此內容
escapeHtml:是否轉義HTML,默認true -->
<s:property value="'<hr/>'" default="當空值時輸出這個" escapeHtml="false"/>
<!-- 查看值棧及Stack Context -->
<s:debug></s:debug>
<!-- 格式化輸出一個日期 -->
<s:date name="currentDate" format="yyyy-MM-dd HH:mm:ss"/>
<p/>
<!-- 定義變量並賦值 -->
<!-- var:變量名
value:變量值
scope:作用域 -->
<s:set var="vipname" value="name" scope="session"></s:set>
<s:property value="#session.vipname"/>
<p/>
<!-- 超鏈接和URL地址 -->
<s:url value="http://www.baidu.com">在頁面上輸出百度網址</s:url>
<br/>
<s:url value="http://www.baidu.com" var="url">把百度網址存入變量url中
<!-- 參數標籤 -->
<!-- value值如果是字符串要加引號
這樣url地址是:'http://www.baidu.com?str=abc'-->
<s:param name="str" value="'abc'"></s:param>
</s:url>
<br/>
<!-- href屬性的值默認是字符串常量,如果使用OGNL表達式,則需要把表達式放在%{}中 -->
<s:a href="%{#url}">百度</s:a>
<%-- 和<jsp:include>一樣都是動態包含,都是被包含的頁面先生成HTML在被包含進來 --%>
<s:include value=""></s:include>
</body>
- 頁面輸出結果
2、控制標籤
- action配置
<action name="Control_tag" class="com.pb06.demo02.HouseAction">
<result>/jsp/06_Struts2標籤/02_控制標籤.jsp</result>
</action>
- action類代碼
1、JavaBean
public class Houses {
private String houseId;
private Street street;
private String title;
private Date addDate;
//構造方法及get、set方法略
}
public class Street {
private String streetName;
//構造方法及get、set方法略
}
2、action類
public class HouseAction extends ActionSupport {
private List<Houses> houses;
@Override
public String execute() throws Exception {
houses = new ArrayList<Houses>();
Street s1 = new Street("街道一");
Street s2 = new Street("街道二");
Date date = new Date(1991, 1, 1, 3, 20, 1);
Houses h1 = new Houses("01", s1, "倆室一廳", date);
date = new Date(2000, 4, 1, 5, 20, 1);
Houses h2 = new Houses("02", s2, "三室一廳", date);
houses.add(h1);
houses.add(h2);
return SUCCESS;
}
public List<Houses> getHouses() {
return houses;
}
public void setHouses(List<Houses> houses) {
this.houses = houses;
}
}
- 頁面代碼(重點)
<body>
<s:set name="score" value="80"></s:set>
<!-- if elseif else -->
<s:if test="#score>=90">
尖子生
</s:if>
<s:elseif test="#score<=60">
普通學生
</s:elseif>
<s:else>
差生
</s:else>
<!-- iterator -->
<table border="1">
<tr>
<td>房屋ID</td>
<td>房屋所處街道</td>
<td>房屋標題</td>
<td>發佈時間</td>
</tr>
<!-- value:被循環訪問的集合,這個屬性的值是個OGNL表達式
var:變量名稱,用來引用當前循環訪問的集合元素
status:返回當前循環的各種信息
count:集合含有多少個對象
index:正在循環的這一項的索引
even:當前遍歷到的對象是否處於列表的偶數索引位置
odd:當前遍歷的對象是否處於列表的奇數索引位置 -->
<s:iterator var="houseItor" value="houses" status="status">
<s:if test="#status.even">
<tr bgcolor="yellow">
</s:if>
<s:if test="#status.odd">
<tr bgcolor="gray">
</s:if>
<td><s:property value="#houseItor.houseId"/></td>
<td><s:property value="#houseItor.street.streetName"/></td>
<td><s:property value="houseItor.title"/></td>
<td><s:date name="#houseItor.addDate" format="yyyy-MM-dd HH:mm:ss"/></td>
</tr>
</s:iterator>
</table>
</body>
- 頁面結果
二、UI標籤
- action配置
<action name="UI_Tags01" class="com.pb06.demo03.Provinces">
<result>/jsp/06_Struts2標籤/03_UI標籤01.jsp</result>
</action>
- action代碼
1、JavaBean
public class Province {
private int pid;
private String pname;
//構造方法及get、set方法略
}
2、action
public class Provinces extends ActionSupport {
List<Province> list;
@Override
public String execute() throws Exception {
list = new ArrayList<Province>();
list.add(new Province(1, "江蘇省"));
list.add(new Province(2, "山東省"));
return SUCCESS;
}
public List<Province> getList() {
return list;
}
public void setList(List<Province> list) {
this.list = list;
}
}
- 頁面代碼(重點)
<body>
<!-- form表單 -->
<!-- name:表單的名稱
action:處理該表單數據的URL
method:提交方式,一般爲get、post -->
<s:form name="" action="" method=""></s:form>
<!-- 單行文本框 -->
<!-- name:文本框名稱
value:文本框中顯示的文字
maxlength:文本框中的最大字符數
readonly:是否爲只讀 -->
<s:textfield name="單行文本框" value="這是一個單行文本框" maxlength="20" readonly="true"></s:textfield>
<p/>
<!-- 多行文本域 -->
<!-- name:文本域名稱
value:文本域中顯示的文字
cols:文本域的寬度,以字符數表示
rows:文本域的高度,以字符行數表示
readonly:是否爲只讀 -->
<s:textarea name="多行文本域" value="這是一個多行文本域" cols="100" rows="20" readonly="false"></s:textarea>
<p/>
<!-- 表單提交按鈕 -->
<s:submit value="提交"></s:submit>
<p/>
<!-- 下拉框 -->
<!-- name:下拉框的名稱
list:生成下拉框的集合
listKey:生成的下拉項的value屬性
listValue:生成的下拉項顯示的文字 -->
<!-- 第一種實現方式 -->
<s:select list="{'第一頁','第二頁','第三頁'}" label="頁碼"></s:select>
<p/>
<!-- 第二種實現方式 -->
<s:select list="list" name="province" listKey="pid" listValue="pname"></s:select>
</body>
- 輸出結果
<s:doubleselect>
聯動下拉框參考:
三、Ajax標籤
struts2提供了一些簡單的Ajax,對於複雜的Ajax實現一般採用jQuery+JSON+Struts2一起來實現
- 日曆控件示例
- 效果