文章摘要
1、Struts 2 url標(biāo)簽
2、Struts 2表單標(biāo)簽
3、Struts 2屬性標(biāo)簽
英文文獻(xiàn)請(qǐng)點(diǎn)擊此處~
在Hello World中,我們添加了一個(gè)Struts 2 url標(biāo)簽index.jsp
來(lái)創(chuàng)建一個(gè)超鏈接hello.action。
<body>
<h1>Welcome To Struts 2!</h1>
<p><a href="<s:url action='hello'/>">Hello World</a></p>
</body>
要在視圖頁(yè)面上使用Struts 2標(biāo)簽,您必須包含一個(gè)標(biāo)簽庫(kù)指令。通常,taglib偽指令是<%@ taglib prefix="s" uri="/struts-tags" %\>。所以所有Struts 2標(biāo)簽的前綴將為“s”。如果要實(shí)際讀取Struts 2標(biāo)簽TLD文件,您可以在Struts 2核心jar的“META-INF”文件夾中找到它。
接下來(lái),我們?cè)?a href="http://www.itdecent.cn/p/0d3bb6c02ff6" target="_blank">Hello World的基礎(chǔ)上,增加標(biāo)簽的使用案例。
一、Struts 2 url標(biāo)簽
Struts 2標(biāo)簽的一個(gè)用途是創(chuàng)建到其他Web資源的鏈接,特別是本地應(yīng)用程序中的其他資源。
雖然HTML為創(chuàng)建超鏈接提供了一個(gè)簡(jiǎn)單的標(biāo)簽,但HTML標(biāo)簽通常需要我們包含重復(fù)、冗余信息。HTML標(biāo)簽也不容易訪問框架提供的動(dòng)態(tài)數(shù)據(jù)。
- 1、Web應(yīng)用程序中非常常見的用例是鏈接到其他頁(yè)面。
在Hello World中,我們hello.action在index.jsp使用Struts 2的URL標(biāo)簽內(nèi)添加了一個(gè)鏈接。
<p><a href="<s:url action='hello'/>">Hello World</a></p>
- 2、使用標(biāo)簽,添加參數(shù)s:param。
<s:url action="hello" var="helloLink">
<s:param name="userName">Bruce Phillips</s:param>
</s:url>
<p><a href="${helloLink}">Hello Bruce Phillips</a></p>
二、Struts 2表單標(biāo)簽
大多數(shù)應(yīng)用程序?qū)⑹褂枚鄠€(gè)數(shù)據(jù)輸入表單。Struts 2標(biāo)簽使得創(chuàng)建輸入表單變得容易。
每個(gè)Struts 2表單標(biāo)簽都有許多屬性來(lái)模擬正常的HTML表單標(biāo)簽屬性。
要?jiǎng)?chuàng)建窗體的外殼,請(qǐng)使用Struts 2表單標(biāo)簽。action屬性設(shè)置要提交的操作名稱。
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html>
<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>Get your own personal hello by filling out and submitting this form.</p>
<s:form action="hello">
<s:textfield name="userName" label="Your name" />
<s:submit value="Submit" />
</s:form>
</body>
</html>
表單標(biāo)簽,在瀏覽時(shí),瀏覽頁(yè)面:
頁(yè)面中的struts 標(biāo)簽,會(huì)轉(zhuǎn)化為html:
<form id="hello" name="hello" action="/using-tags/hello.action;jsessionid=6233ot11na1mtshbr292hu1w" method="post">
<table class="wwFormTable">
<tr>
<td class="tdLabel"><label for="hello_userName" class="label">Your name:</label></td>
<td class="tdInput"><input type="text" name="userName" value="" id="hello_userName"/></td>
</tr>
<tr>
<td colspan="2">
<div class="formButton">
<input type="submit" value="Submit" id="hello_0"/>
</div>
</td>
</tr>
</table>
</form>
三、Struts 2屬性標(biāo)簽
- 1、屬性標(biāo)簽:
<s:property value="messageStore.message" />
屬性標(biāo)簽最常見的用法是通過(guò)調(diào)用公共get方法(Action類)“獲取”返回的值,然后將該值包含在返回給瀏覽器的HTML中。
如Hello World教程中所討論的,messageStore.message指示Struts 2首先調(diào)用getMessageStore Action類的方法的值。該方法調(diào)用返回一個(gè)MessageStore對(duì)象。該.message部分指示Struts 2調(diào)用MessageStore對(duì)象的getMessage方法。該getMessage方法返回一個(gè)String,該字符串將包含在返回給瀏覽器的HTML中。
Struts 2屬性標(biāo)簽的一個(gè)非常有用的功能是它會(huì)自動(dòng)將最常見的數(shù)據(jù)類型(int,double,boolean)轉(zhuǎn)換為其等效字符串。為了演示此功能,我們將一個(gè)靜態(tài)int變量添加到類中HelloWorldAction。
HelloWorldAction.java
package wanghailu.apache.struts.action;
import com.opensymphony.xwork2.ActionSupport;
import wanghailu.apache.struts.model.MessageStore;
public class HelloWorldAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private MessageStore messageStore;
private static int helloCount = 0;
public int getHelloCount() {
return helloCount;
}
public String execute() throws Exception {
messageStore = new MessageStore() ;
helloCount++;
return SUCCESS;
}
public MessageStore getMessageStore() {
return messageStore;
}
}
每次調(diào)用execute方法時(shí),我們將增加helloCount1.所以將這個(gè)代碼添加到類的execute方法中HelloWorldAction.
每當(dāng)用戶單擊頁(yè)面index.jsp上的鏈接之一(或提交表單)時(shí),將運(yùn)行execute類的方法,HelloWorldAction并將靜態(tài)字段helloCount增加1。
要包含helloCount屬性的值,HelloWorld.jsp我們可以使用Struts 2屬性標(biāo)記。
- 2、使用屬性標(biāo)簽顯示helloCount值:
<p>I've said hello <s:property value="helloCount" /> times!</p>
所以即使該getHelloCount方法返回一個(gè)整數(shù)類型,Struts 2將其轉(zhuǎn)換為String類型并將其放入p標(biāo)簽的正文中。
請(qǐng)注意,即使helloCount是靜態(tài)字段,get方法helloCount也不是靜態(tài)的。對(duì)于Struts 2調(diào)用getHelloCount方法來(lái)獲取值helloCount,該getHelloCount方法不能是靜態(tài)的。
如果get方法返回的值是一個(gè)對(duì)象,那么屬性標(biāo)簽將導(dǎo)致Struts 2調(diào)用該對(duì)象的toString方法。當(dāng)然,您應(yīng)該始終toString在模型類中覆蓋Class Object的方法。將以下toString方法添加到MessageStore類中:
public String toString() {
return message + " (from toString)";
}
- 3、使用屬性調(diào)用toString:
<p><s:property value="messageStore" /></p>
由于getMessageStore的HelloWorldAction類返回MessageStore對(duì)象,搭片2將調(diào)用toString類的方法MessageStore。該toString方法返回的字符串將顯示在瀏覽器中。


