避免被爬,先貼上本帖地址:struts2 官方系列教程一:使用struts2 標簽 tag http://www.cnblogs.com/linghaoxinpian/p/6901316.html
本教材假定你已完成了HelloWorld項目,你可以 下載本章節的代碼
在上一節教程中,我們在index.jsp中使用 url tag 創建了一個超鏈接hello.action 這節我們將探索struts2中其它tags
Web應用程序與傳統網站不同,Web應用程序可以創建動態響應。為了更方便地引用頁面上的動態數據,Struts 2框架提供了一系列標簽(tag)。有些標簽模仿標准的HTML標簽,同時提供了一個value屬性,有些標簽創建是非標准的但卻非常有用。
為了使用struts2 tag,我們必須首先引入一個taglib庫指令 通常這個指令是這樣的:<%@ taglib prefix="s" uri="/struts-tags" %\> 這樣所有的struts2 tag 將會以 “s”為前綴,如果你想閱讀一下 struts2 tag的TLD文件,你可以在 Struts 2 core jar里的META-INF 文件夾里找到。
Struts 2 url Tag(tag即標簽,以下文章可能會兩者都用,都是一個意思)
雖然HTML為創建超鏈接提供了一個簡單的標簽:a標簽,但是a標簽通常包含冗余信息。此外,HTML標簽不是很容易就能動態訪問框架提供的動態數據。一個常見的例子是鏈接到其他頁面。在系列二中,我們使用 url tag在index.jsp中添加了一個鏈接到hello.action。有關url tag的更多信息可以參考 url documentation
index.jsp
<!DOCTYPE html> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> <%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Basic Struts 2 Application - Welcome</title> </head> <body> <h1>Welcome To Struts 2!</h1> <p><a href="<s:url action='hello'/>">Hello World</a></p> </body> </html>
一個常見的用例是,URL還需要包含一個查詢字符串參數,比如userName。如果要添加一個查詢字符串參數,則使用Struts2的 param標記,嵌套在url標記內。
帶參數的url tag
<!DOCTYPE html> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> <%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Basic Struts 2 Application - Welcome</title> </head> <body> <h1>Welcome To Struts 2!</h1> <p><a href="<s:url action='hello'/>">Hello World</a></p> <!--url tag--> <s:url action="hello" var="helloLink"> <s:param name="userName">零號芯片</s:param> </s:url> <p><a href="${helloLink}">Hello 芯片</a></p> </body> </html>
然后將url tag作為a標簽的href屬性值,我們將s:url tag分離到它自己的代碼塊中。我們可以從上面的代碼中看出,嵌套在 url tag 中的是param tag,這個 tag允許我們指定一個參數名name:userName,和參數值:零號芯片。注意參數值將會被進行URL編碼,在下一個教程中,我們將討論如何在struts2中訪問參數值。
注意url tag中var屬性的使用。var屬性的值是一個引用,我們可以在代碼中使用它來引用創建的url,上面代碼中,a標簽的href即var屬性值。
Struts 2 Form Tag標簽
多數Web Application都會使用多表單錄入數據,struts2標簽(tag)使得創建表單更加容易,我們在index.jsp中添加如下內容,你可以參考Form Tags Reference來了解struts2 form標簽的更多詳情信息。
1 <!DOCTYPE html> 2 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> 3 <%@ taglib prefix="s" uri="/struts-tags" %> 4 <html> 5 <head> 6 <meta charset="UTF-8"> 7 <title>Basic Struts 2 Application - Welcome</title> 8 </head> 9 <body> 10 <h1>Welcome To Struts 2!</h1> 11 <p><a href="<s:url action='hello'/>">Hello World</a></p> 12 <!--url tag--> 13 <s:url action="hello" var="helloLink"> 14 <s:param name="userName">零號芯片</s:param> 15 </s:url> 16 <p><a href="${helloLink}">Hello 芯片</a></p> 17 <!-- form tag --> 18 <p>Get your own personal hello by filling out and submitting this form.</p> 19 <s:form action="hello"> 20 <s:textfield name="userName" label="Your name" /> 21 <s:submit value="Submit" /> 22 </s:form> 23 </body> 24 </html>
textfield標簽會創建一個input文本框(而里面label屬性會創建一個label標簽),submit標簽會創建一個submit提交按鈕,如下所示:

來來來,看一下源碼是這樣子的,為了讓我們能看清,特地用sublime格式化了一下代碼。

好吧,這看上去是什么鬼!??一臉懵。但有一點,這個form表單是提交給hello.action無疑的。
我們注意到,struts2在form標簽里創建了一個table來定位 label、input、submit這三個標簽。在后面的教程中,我們將學習如何指定布局(table、CSS)。在下一篇教程中,將介紹如何使用Struts 2來處理這種表單的提交。
Struts 2 property tag標簽
在 struts2入門系列二之Hello World 中的HelloWorld.jsp,我們使用了這樣的一句話:
<s:property value="messageStore.message" />
一個常用的使用方式是調用Action中的公開getter方法獲取值作為value屬性的屬性值,然后struts2會將這個值替代property標簽返回給瀏覽器。
這里在說一次,就一次咯,正如在Hello World教程中所討論的,messageStore.message指示Struts2到Action類去第一次調用getMessageStore()方法。該方法調用返回一個MessageStore對象 而 .message部分指示Struts 2調用MessageStore對象的getMessage()方法。getMessage方法返回一個字符串,該字符串將包含在返回到瀏覽器的HTML中。
Struts2 property標簽的一個非常有用的特性是它將自動地將最常用的數據類型(int、double、boolean)轉換為字符串等價值。為了演示這個特性,讓我們向HelloWorldAction類中添加一個靜態int變量。
1 package action; 2 3 import com.opensymphony.xwork2.ActionSupport; 4 5 import model.MessageStore; 6 7 public class HelloWorldAction extends ActionSupport { 8 private MessageStore messageStore; 9 10 public String execute() { 11 messageStore = new MessageStore() ; 12 13 return SUCCESS; 14 } 15 16 public MessageStore getMessageStore() { 17 return messageStore; 18 } 19 //添加一個static int變量 20 private static int helloCount = 0; 21 22 public int getHelloCount() { 23 return helloCount; 24 } 25 }
每次execute()方法被調用時,我們便讓helloCount++
1 package action; 2 3 import com.opensymphony.xwork2.ActionSupport; 4 5 import model.MessageStore; 6 7 public class HelloWorldAction extends ActionSupport { 8 private MessageStore messageStore; 9 10 public String execute() { 11 //每次調用helloCount++ 12 helloCount++; 13 messageStore = new MessageStore() ; 14 15 return SUCCESS; 16 } 17 18 public MessageStore getMessageStore() { 19 return messageStore; 20 } 21 //添加一個static int變量 22 private static int helloCount = 0; 23 24 public int getHelloCount() { 25 return helloCount; 26 } 27 }
在HelloWorld.jsp的h2標簽下添加property標簽
1 <!DOCTYPE html> 2 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> 3 <%@ taglib prefix="s" uri="/struts-tags" %> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>Hello World!</title> 8 </head> 9 <body> 10 <h2><s:property value="messageStore.message" /></h2> 11 <!-- 顯示helloCount的值 --> 12 <p>I've said hello <s:property value="helloCount" /> times!</p> 13 </body> 14 </html>
這樣getHelloCount()方法返回一個整數類型,Struts 2將它轉換為類型字符串,並將其放入p標簽的主體中。
注意:盡管helloCount是靜態的,但它的getter方法不是靜態的,對於struts2來說,getter方法必須不能是靜態的!!
如果方法返回值是一個object類型,那么就會調用該類型的toString()方法,Of course,我們應該總是重寫override模型類的toString()方法。在MessageStore類中添加toString()方法,如下:
public String toString() { return message + " (from toString)"; }
繼續在HelloWorld.jsp中添加如下代碼:
<p><s:property value="messageStore" /></p>
運行如下:

我們在本教程中介紹了很多,但是我們只討論了如何使用Struts 2標簽。有關Struts 2標記的更多信息,請參閱Struts 2 Tag Reference。
