開發(fā)工具: MyEclipse6
Web服務(wù)器: Tomcat6
Struts版本: Struts2.0.11.1
JDK版本: JDK1.5.0_12
J2EE版本: Java EE5.0
在本系列教程中 Web工程的上下文路徑都是 struts2,如果在 Web根目錄有一個 index.jsp文件,則訪問路徑如下:
http://localhost:8080/struts2/index.jsp
由于 MyEclipse6目前并不支持 Struts2,所以我們需要到 struts.apache.org去下載 Struts2安裝包。要想正常使用 Struts2,至少需要如下五個包(可能會因為 Struts2的版本不同,包名略有差異,但包名的前半部是一樣的)。
struts2-core-2.0.11.1.jar
xwork-2.0.4.jar
commons-logging-1.0.4.jar
freemarker-2.3.8.jar
ognl-2.6.11.jar
Struts2雖然在大版本號上是第二個版本,但基本上在配置和使用上已經(jīng)完全顛覆了 Struts1.x的方式(當(dāng)然, Struts2仍然是基于 MVC模式的,也是動作驅(qū)動的,可能這是唯一沒變的東西)。 Struts2實際上是在 Webwork基礎(chǔ)上構(gòu)建起來的 MVC框架。我們從 Struts2的源代碼中可以看到,有很多都是直接使用的 xwork(Webwork的核心技術(shù) )的包。既然從技術(shù)上來說 Struts2是全新的框架,那么就讓我們來學(xué)習(xí)一下這個新的框架的使用方法。
如果大家使用過 Struts1.x,應(yīng)該對建立基于 Struts1.x的 Web程序的基本步驟非常清楚。讓我們先來回顧一下建立基于 Struts1.x的 Web程序的基本步驟。
1. 安裝 Struts。由于 Struts的入口點是 ActionServlet,所以得在 web.xml中配置一下這個 Servlet。
2. 編寫 Action類(一般從 org.apache.struts.action.Action類繼承)。
3. 編寫 ActionForm類(一般從 org.apache.struts.action.ActionForm類繼承),這一步不是必須的,如果要接收客戶端提交的數(shù)據(jù),需要執(zhí)行這一步。
4. 在 struts-config.xml文件中配置 Action和 ActionForm。
5. 如果要 采集用戶錄入的數(shù)據(jù),一般需要編寫若干 JSP頁面,并通過這些 JSP頁面中的 form將數(shù)據(jù)提交給 Action。
下面我們就按著編寫 struts1.x程序的 這五步和 struts2.x程序的編寫過程一一對應(yīng),看看它們誰更“酷”。下面我們來編寫一個基于 Struts2的 Web程序。這個程序的功能是讓用戶錄入兩個整數(shù),并提交給一個 Struts Action,并計算這兩個數(shù)的代數(shù)和,如果代碼和為非負(fù)數(shù),則跳轉(zhuǎn)到 positive.jsp頁面,否則跳轉(zhuǎn)到 negative.jsp頁面。
【第 1 步】 安裝 Struts2
這一步對于 Struts1.x和 Struts2都是必須的,只是安裝的方法不同。 Struts1的入口點是一個 Servlet,而 Struts2的入口點是一個過濾器 (Filter)。因此, Struts2要按過濾器的方式配置。下面是在 web.xml中配置 Struts2的代碼:
【第 2 步】 編寫 Action類
這一步和 Struts1.x 也必須進(jìn)行。只是 Struts1.x 中的動作類必須從 Action 類中繼承,而 Struts2.x 的動作類需要從 com.opensymphony.xwork2.ActionSupport 類繼承。下面是計算兩個整數(shù)代碼和的 Action 類,代碼如下: 從上面的代碼可以看出,動作類的一個特征就是要覆蓋 execute方法,只是 Struts2的 execute方法沒有參數(shù)了,而 Struts1.x的 execute方法有四個參數(shù)。而且 execute方法的返回值也不同的。 Struts2只返回一個 String,用于表述執(zhí)行結(jié)果(就是一個標(biāo)志)。上面代碼的其他部分將在下面講解。 【第 3 步】 編寫 ActionForm類 在本例中當(dāng)然需要使用 ActionForm了。在 Struts1.x中,必須要單獨建立一個 ActionForm類(或是定義一個動作 Form),而在 Struts2中 ActionForm和 Action已經(jīng)二合一了。從第二步的代碼可以看出,后面的部分就是應(yīng)該寫在 ActionForm類中的內(nèi)容。所以在第 2步,本例的 ActionForm類已經(jīng)編寫完成(就是 Action類的后半部分)。 【第 4 步】 配置 Action類 在 <struts>標(biāo)簽中可以有多個 <package>,第一個 <package>可以指定一個 Servlet訪問路徑(不包括動作名),如“ /mystruts”。 extends屬性繼承一個默認(rèn)的配置文件“ struts-default”,一般都繼承于它,大家可以先不去管它。 <action>標(biāo)簽中的 name屬性表示動作名, class表示動作類名。 <result>標(biāo)簽的 name實際上就是 execute方法返回的字符串,如果返回的是“ positive”,就跳轉(zhuǎn)到 positive.jsp頁面,如果是“ negative”,就跳轉(zhuǎn)到 negative.jsp頁面。在 <struts>中可以有多個 <package>,在 <package>中可以有多個 <action>。我們可以用如下的 URL來訪問這個動作: 注 : Struts1.x的動作一般都以 .do結(jié)尾,而 Struts2是以 .action結(jié)尾。 【第 5 步】 編寫用戶錄入接口( JSP頁面) 1. 主界面( sum.jsp ) 在 Web根目錄建立一個 sum.jsp,代碼如下: 在 sum.jsp中使用了 Struts2帶的 tag。在 Struts2中已經(jīng)將 Struts1.x的好幾個標(biāo)簽庫都統(tǒng)一了,在 Struts2中只有一個標(biāo)簽庫 /struts-tags。這里面包含了所有的 Struts2標(biāo)簽。但使用 Struts2的標(biāo)簽大家要注意一下。在 <s:form>中最好都使用 Struts2標(biāo)簽,盡量不要用 HTML或普通文本,大家可以將 sum.jsp的代碼改為如下的形式,看看會出現(xiàn)什么效果: ... ... 求代數(shù)和 <br/> <s:form action="mystruts/sum.action" > 操作數(shù) 1: <s:textfield name="operand1" /><br/> 操作數(shù) 2: <s:textfield name="operand1" /><br/> <s:submit value="代數(shù)和 " /> </s:form> ... ... 提示一下,在 <s:form>中 Struts2使用 <table>定位。 2. positive.jsp 3. negative.jsp 這兩個 jsp頁面的實現(xiàn)代碼基本一樣,只使用了一個 <s:property>標(biāo)簽來顯示 Action類中的 sum屬性值。 <s:property>標(biāo)簽是從 request對象中獲得了一個對象中得到的 sum屬性,如我們可以使用如下的代碼來代替 <s:property value=”sum”/>: <% com.opensymphony.xwork2.util.OgnlValueStack ovs = (com.opensymphony.xwork2.util.OgnlValueStack)request.getAttribute("struts.valueStack"); out.println(ovs.findString("sum")); %> 啟動 Tomcat后,在 IE中輸入如下的 URL來測試這個例子:
import com.opensymphony.xwork2.ActionSupport;
public class FirstAction extends ActionSupport
{
private int operand1;
private int operand2;
public String execute() throws Exception
{
if (getSum() >= 0 ) // 如果代碼數(shù)和是非負(fù)整數(shù),跳到positive.jsp頁面
{
return " positive " ;
}
else // 如果代碼數(shù)和是負(fù)整數(shù),跳到negative.jsp頁面
{
return " negative " ;
}
}
public int getOperand1()
{
return operand1;
}
public void setOperand1( int operand1)
{
System.out.println(operand1);
this .operand1 = operand1;
}
public int getOperand2()
{
return operand2;
}
public void setOperand2( int operand2)
{
System.out.println(operand2);
this .operand2 = operand2;
}
public int getSum()
{
return operand1 + operand2; // 計算兩個整數(shù)的代碼數(shù)和
}
}
<! DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd" >
< struts >
< package name ="struts2" namespace ="/mystruts"
extends ="struts-default" >
< action name ="sum" class ="action.FirstAction" >
< result name ="positive" > /positive.jsp </ result >
< result name ="negative" > /negative.jsp </ result >
</ action >
</ package >
</ struts >
http://localhost:8080/struts2/mystruts/sum.action
<% @ taglib prefix = " s " uri = " /struts-tags " %>
< html >
< head >
< title > 輸入操作數(shù) </ title >
</ head >
< body >
求代數(shù)和
< br />
< s:form action ="mystruts/sum.action" >
< s:textfield name ="operand1" label =" 操作數(shù)1" />
< s:textfield name ="operand2" label =" 操作數(shù)2" />
< s:submit value ="代數(shù)和" />
</ s:form >
</ body >
</ html >
<% @ taglib prefix = " s " uri = " /struts-tags " %>
< html >
< head >
< title > 顯示代數(shù)和 </ title >
</ head >
< body >
代數(shù)和為非負(fù)整數(shù) < h1 >< s:property value ="sum" /></ h1 >
</ body >
</ html >
<% @ taglib prefix = " s " uri = " /struts-tags " %>
< html >
< head >
< title > 顯示代數(shù)和 </ title >
</ head >
< body >
代數(shù)和為負(fù)整數(shù) < h1 >< s:property value ="sum" /></ h1 >
</ body >
</ html >
http://localhost:8080/struts2/sum.jsp