Struts2標籤庫提供了主題、模板支持,極大地簡化了視圖頁面的編寫,並且,struts2的主題、模板都提供了很好的擴展性,實現了更好的代碼複用。
Struts2容許在頁面中使用自定義組件,這徹底能知足項目中頁面顯示覆雜,多變的需求。
Struts2的標籤庫有一個巨大的改進之處,struts2標籤庫的標籤不依賴於任何表現層技術,也就是說strtus2提供了大部分標籤,能夠在各類表現技術中使用。
包括最經常使用的jsp頁面,也能夠說Velocity和FreeMarker等模板技術中的使用。html
1.用戶界面標籤(UI標籤):主要用來生成HTML元素的標籤。java
(1)表單標籤:主要用於生成HTML頁面的FORM元素,以及普通表單元素的標籤。web
(2)非表單標籤:主要用於生成頁面上的tree,Tab頁等。apache
2.非用戶界面標籤(非UI標籤):主要用於數據訪問,邏輯控制。數組
(1)數據訪問標籤:主要包含用於輸出值棧(ValueStack)中的值,完成國際化等功能的標籤。session
(2)流程控制標籤:主要包含用於實現分支,循環等流程控制的標籤。app
3.AJAX標籤:用於支持Ajax效果jsp
使用時須要在jsp頁面中引入標籤:ide
<%@ taglib prefix="s" uri="/struts-tags"%>
須要在web.xml中添加使用struts的配置信息。函數
一、<s:property/>標籤:用來輸出一個值棧屬性值
名字 | 類型 | 默認值 | 說明 |
default | String | 可選,若value值爲null或者沒有給定,將顯示該屬性值 | |
escape | boolean | true | 可選,是否要對html特殊字符進行轉義 |
value | String | <來自棧頂對象> | 將要顯示的值 |
index.jsp:
<a href="strutstags.action?name=hello name">send parameter link</a>
comon-tags.jsp
s:property:打印對象棧中的屬性值<br> <s:property value="tagName"></s:property><br> map棧:對於map棧,打印request,session,application的某個屬性值或者某個請求參數的值。<br> <s:property value="#session.date"></s:property><br> <s:property value="#parameters.name[0]"></s:property><br>
struts.xml
<action name="strutstags" class="com.dx.struts2.StrutsTags" method="test"> <result name="test">/comon-tags.jsp</result> </action>
SturtsTags.java action類:
/** * @author Administrator * */ package com.dx.struts2; import java.util.Date; import java.util.Map; import org.apache.struts2.interceptor.SessionAware; public class StrutsTags implements SessionAware{ private String tagName; public String getTagName() { return tagName; } public void setTagName(String tagName) { this.tagName = tagName; } public String test(){ this.tagName="Hello Struts-tags"; this.session.put("date", new Date()); return "test"; } private Map<String,Object> session; @Override public void setSession(Map<String, Object> session) { // TODO Auto-generated method stub this.session=session; } }
輸出結果:
s:property:打印對象棧中的屬性值
Hello Struts-tags
map棧:對於map棧,打印request,session,application的某個屬性值或者某個請求參數的值。
17-3-20
hello name
二、url標籤用來動態地建立一個URL
名字 | 類型 | 默認值 | 說明 |
action | String | 可選,指定生成的url爲哪一個action | |
anchor | String | 可選,指定被建立的url的連接錨點 | |
encode | Boolean | true | 可選,是否對參數進行編碼 |
escapeAmp | Boolean | true | 可選,是否要對「&」字符進行轉義 |
includeContext | Boolean | true | 可選,是否要把當前的上下文包括進來 |
includeParams | String | get | 可選,指定是否包含請求參數,能夠取3個值之一:one/get/all |
method | String | 可選,指定action的方法,當用action屬性來生成url是,若是指定該屬性,url將連接到指定的action的方法上。 | |
namespace | String | 可選,指定url的命名空間 | |
portletMode | String | 可選,指定結果頁面的portlet模式 | |
protletUrlType | String | 可選,指定將被建立的URL是一個protlet例程,仍是action URL | |
schema | String | 可選,指定使用什麼協議:http,https | |
value | String | 可選,指定將生成的url值(若是新建URL的不是一個action的話) | |
var | String | 可選,指定用來被壓入ContextMap中的鍵值 | |
windowState | String | 可選,當用在一個portlet環境時,用來指定portlet的窗口狀態 |
comon-tags.jsp
s:url 建立一個url地址<br> <s:url value="/helloValue" var="url1"> <!-- 對於Value值會自動動態賦值 --> <s:param name="tagInteger" value="111"/> </s:url> ${url1}<br> <s:url value="/helloValue" var="url2"> <!-- 對於Value值會自動的進行OGNL解析 --> <s:param name="tagInteger" value="tagInteger"/> </s:url> ${url2}<br> <s:url value="/helloValue" var="url3"> <!-- 對於Value值會自動的進行OGNL解析,若不但願進行解析,則須要使用單引號引着 --> <s:param name="tagInteger" value="'abc'"/> </s:url> ${url3}<br> <s:url action="helloAction" namespace="/hellonamespace" method="save" var="url4"> <s:param name="tagInteger" value="'abcde'"/> </s:url> ${url4}<br> <s:url value="/testUrl" var="url5" includeParams="get"> </s:url> ${url5}<br> <!-- post方式提交的參數 --> <s:url value="/testUrl" var="url6" includeParams="all"> </s:url> ${url6}<br>
備註:上邊的s:param 中也支持ognl寫法好比:
<s:url value="/testUrl" var="url7"> <s:param name="date" value="#session.date"></s:param> </s:url> <br>
index.jsp
<s:form action="strutstags.action" method="post"> product name: <input name="productName" /> <br /> product description: <input name="productDesc" /> <br /> product price: <input name="productPrice" /> <br /> <s:submit value="提交"></s:submit> </s:form> <br> <a href="strutstags.action?name=hello name">send parameter link</a>
ComonTags.java
/** * @author Administrator * */ package com.dx.struts2; import java.util.Date; import java.util.Map; import org.apache.struts2.interceptor.SessionAware; public class StrutsTags implements SessionAware{ private Integer tagInteger; private String tagName; public String getTagName() { return tagName; } public void setTagName(String tagName) { this.tagName = tagName; } public Integer getTagInteger() { return tagInteger; } public void setTagInteger(Integer tagInteger) { this.tagInteger = tagInteger; } public String test(){ this.tagName="Hello Struts-tags"; this.tagInteger=8888; this.session.put("date", new Date()); return "test"; } private Map<String,Object> session; @Override public void setSession(Map<String, Object> session) { // TODO Auto-generated method stub this.session=session; } }
當經過點擊index.jsp中的"send parameter link"時,comon-tags.jsp顯示效果:
s:url 建立一個url地址
/Struts_01/helloValue?tagInteger=111
/Struts_01/helloValue?tagInteger=8888
/Struts_01/helloValue?tagInteger=abc
/Struts_01/hellonamespace/helloAction!save.action?tagInteger=abcde
/Struts_01/testUrl?name=hello+name
/Struts_01/testUrl?name=hello+name
當經過點擊index.jsp的"提交"按鈕時,comon-tags.jsp顯示效果:
s:url 建立一個url地址
/Struts_01/helloValue?tagInteger=111
/Struts_01/helloValue?tagInteger=8888
/Struts_01/helloValue?tagInteger=abc
/Struts_01/hellonamespace/helloAction!save.action?tagInteger=abcde
/Struts_01/testUrl
/Struts_01/testUrl?productName=1&productDesc=2&productPrice=3
三、set 標籤 用來在如下Map對象裏建立一個鍵值對:
------ValueStack值棧的ContextMap值棧
------Map類型的session對象
------Map類型application對象
------Map類型的request對象
------Map類型的page對象
名字 | 類型 | 默認值 | 說明 |
name | String | 將被建立的屬性的鍵 | |
value | String | 該鍵新引用的對象 | |
scope | String | default | 目標變量的做用範圍,可取值是application/session/request/page和default |
頁面
<!-- 會對value值自動的進行OGNL解析 --> <s:set name="mySet1" value="tagInteger" scope="request"></s:set><br> ${mySet1}<br> ${requestScope.mySet1 }<br>
輸出結果:
8888
8888
四、push標籤
push標籤的功能和set標籤相似;
push標籤將把一個對象壓入ValueStack,而不是壓入ContextMap;
push標籤在標籤起始是把一個對象壓入棧,標籤結束時將對象彈出棧。
名字 | 類型 | 默認值 | 說明 |
Value | String | 將壓入ValueStack棧的值 |
頁面:
s:push<br> <s:push value="#request.people"> name:${name}-age:${age } </s:push><br> name:${name}-age:${age }<br>
ComonTags.java action
/** * @author Administrator * */ package com.dx.struts2; import java.util.Date; import java.util.Map; import org.apache.struts2.interceptor.RequestAware; import org.apache.struts2.interceptor.SessionAware; public class StrutsTags implements SessionAware , RequestAware{ private Integer tagInteger; private String tagName; public String getTagName() { return tagName; } public void setTagName(String tagName) { this.tagName = tagName; } public Integer getTagInteger() { return tagInteger; } public void setTagInteger(Integer tagInteger) { this.tagInteger = tagInteger; } public String test(){ this.tagName="Hello Struts-tags"; this.tagInteger=8888; this.session.put("date", new Date()); People people=new People(); people.setName("people1"); people.setAge(10); this.request.put("people", people); return "test"; } private Map<String,Object> session; @Override public void setSession(Map<String, Object> session) { this.session=session; } private Map<String,Object> request; @Override public void setRequest(Map<String, Object> request) { this.request= request; } }
Peopel.java
package com.dx.struts2; public class People { private String name; private Integer age; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } }
輸出結果:
s:push
name:people1-age:10
name:-age:
五、if,else 和elseif標籤
test屬性必須
s:if s:elseif s:else<br> ${requestScope.people.age }<br> <s:if test="#request.people.age>80">>80</s:if> <s:elseif test="#request.people.age>30">>30</s:elseif> <s:elseif test="#request.people.age>8">>8</s:elseif> <s:else><=8</s:else> <br> <s:if test="#request.people.age>10">>10</s:if> <s:else><=10</s:else> <br>
測試結果:
s:if s:elseif s:else
10
>8
<=10
六、iterator標籤
iterator標籤用來遍歷一個數組,Collection或一個Map,並把這個可遍歷對象裏的一個元素依次壓入和彈出ValueStack棧
名字 | 類型 | 默認值 | 說明 |
value | String | 將被遍歷是能夠遍歷對象 | |
status | org.apache.struts2.views.jsp.IteratorStatus | ||
var | String | 用來引用這個可遍歷對象中的當前元素的變量 |
在開始執行時,iterator標籤會先把IteratorStatus類的一個實例壓入ContextMap,並把每次循環遍歷時更新它,能夠將一個指向IteratorStatus對象的變量賦值給status.
StrutsTags.java的test方法添加代碼:
List<People> peoples=new ArrayList<People>(); peoples.add(new People("AAA",111)); peoples.add(new People("BBB",222)); peoples.add(new People("CCC",333)); peoples.add(new People("DDD",444)); peoples.add(new People("EEE",555)); this.request.put("peoples", peoples);
修改People.java添加構造函數:
package com.dx.struts2; public class People { private String name; private Integer age; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public People(){} public People(String name,Integer age){ this.name=name; this.age=age; } }
comon-tags.jsp
s:iterator<br> <s:iterator value="#request.peoples" status="status"> index:${status.index }-count:${status.count }:${name }-${age }<br> </s:iterator><br>
輸出結果:
s:iterator
index:0-count:1:AAA-111
index:1-count:2:BBB-222
index:2-count:3:CCC-333
index:3-count:4:DDD-444
index:4-count:5:EEE-555
六、sort標籤:用來對一個可遍歷的對象裏的元素進行排序
名稱 | 類型 | 默認值 | 說明 |
comparator | java.util.Comparator | 在排序過程當中使用的比較器 | |
source | String | 將對之進行排序的可遍歷對象 | |
var | String | 用來應用因排序而新生成的可遍歷對象的變量 |
添加PeopleComparator.java
package com.dx.struts2; import java.util.Comparator; public class PeopleComparator implements Comparator<People>{ @Override public int compare(People o1, People o2) { return o1.getName().compareTo(o2.getName()); } }
修改ComonTags.java:
/** * @author Administrator * */ package com.dx.struts2; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Map; import org.apache.struts2.interceptor.RequestAware; import org.apache.struts2.interceptor.SessionAware; public class StrutsTags implements SessionAware, RequestAware { private Integer tagInteger; private String tagName; private PeopleComparator peopleComparator; public String getTagName() { return tagName; } public void setTagName(String tagName) { this.tagName = tagName; } public Integer getTagInteger() { return tagInteger; } public void setTagInteger(Integer tagInteger) { this.tagInteger = tagInteger; } public PeopleComparator getPeopleComparator() { return peopleComparator; } public void setPeopleComparator(PeopleComparator peopleComparator) { this.peopleComparator = peopleComparator; } public String test() { this.tagName = "Hello Struts-tags"; this.tagInteger = 8888; this.peopleComparator=new PeopleComparator(); this.session.put("date", new Date()); People people = new People(); people.setName("people1"); people.setAge(10); this.request.put("people", people); List<People> peoples = new ArrayList<People>(); peoples.add(new People("AAA", 111)); peoples.add(new People("DDD", 444)); peoples.add(new People("CCC", 333)); peoples.add(new People("EEE", 555)); peoples.add(new People("BBB", 222)); this.request.put("peoples", peoples); return "test"; } private Map<String, Object> session; @Override public void setSession(Map<String, Object> session) { this.session = session; } private Map<String, Object> request; @Override public void setRequest(Map<String, Object> request) { this.request = request; } }
修改comon-tags.jsp:
s:sort<br> <s:sort comparator="peopleComparator" source="#request.peoples" var="peoples2"> </s:sort> <s:iterator value="#attr.peoples2"> ${name }-${age }<br> </s:iterator>
測試結果:
s:sort
AAA-111
BBB-222
CCC-333
DDD-444
EEE-555
七、date標籤
名字 | 類型 | 默認值 | 說明 |
format | String | 可選,日期的格式 | |
name | String | 將被排版的日期值 | |
nice | Boolean | false | 可選,指定是否輸出日期和當前日期之間的時間差 |
var | String | 可選,用來引用被壓入ValueStack棧的日期值的變量 |
format屬性值必須是java.util.SimpleDateFormat類裏定義的日期、時間格式之一。
修改頁面:
s:date<br> <s:date name="#session.date" format="yyyy-MM-dd HH:mm:ss" var="date23"></s:date> ${date23 }
輸出結果:
s:date
2017-03-20 21:53:19
八、a標籤
a標籤將呈現爲一個html連接,這個標籤能夠接受HTML語言中的a元素所能接受的全部屬性。
修改頁面代碼:
a:標籤<br> <s:iterator value="#request.peoples"> <s:a href="/myTest.action?name=%{name}">${age }</s:a><br> </s:iterator>
生成結果: