Struts2教程:第一個Struts2程序
在本系列教程中我們將學(xué)習(xí)到Struts2的各種技術(shù)。在本教程中使用的工具和程序庫的版本如下:
開發(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的代碼:
- < filter>
- < filter-name>struts2< /filter-name>
- < filter-class>
- org.apache.struts2.dispatcher.FilterDispatcher
- < /filter-class>
- < /filter>
- < filter-mapping>
- < filter-name>struts2< /filter-name>
- < url-pattern>/*< /url-pattern>
- < /filter-mapping>
【第2步】 編寫Action類
這一步和Struts1.x也必須進(jìn)行。只是Struts1.x中的動作類必須從Action類中繼承,而Struts2.x的動作類需要從com.opensymphony.xwork2.ActionSupport類繼承。下面是計算兩個整數(shù)代碼和的Action類,代碼如下:
- package action;
- 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ù)和
- }
- }
從上面的代碼可以看出,動作類的一個特征就是要覆蓋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類
這一步struts1.x和struts2.x都是必須的,只是在struts1.x中的配置文件一般叫struts-config.xml(當(dāng)然也可以是其他的文件名),而且一般放到WEB-INF目錄中。而在struts2.x中的配置文件一般為struts.xml,放到WEB-INF"classes目錄中。下面是在struts.xml中配置動作類的代碼:
- < ?xml version="1.0" encoding="UTF-8" ?>
- < !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>
在< 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來訪問這個動作:
http://localhost:8080/struts2/mystruts/sum.action
注:Struts1.x的動作一般都以.do結(jié)尾,而Struts2是以.action結(jié)尾。
【第5步】 編寫用戶錄入接口(JSP頁面)
1. 主界面(sum.jsp)
在Web根目錄建立一個sum.jsp,代碼如下:
- < %@ page language="java" import="java.util.*" pageEncoding="GBK" %>
- < %@ 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>
在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
- < %@ page language="java" import="java.util.*" pageEncoding="GBK"%>
- < %@ taglib prefix="s" uri="/struts-tags" %>
- < html>
- < head>
- < title>顯示代數(shù)和< /title>
- < /head>
- < body>
- 代數(shù)和為非負(fù)整數(shù)< h1>< s:property value="sum" />< /h1>
- < /body>
- < /html>
3. negative.jsp
- < %@ page language="java" import="java.util.*" pageEncoding="GBK"%>
- < %@ taglib prefix="s" uri="/struts-tags" %>
- < html>
- < head>
- < title>顯示代數(shù)和< /title>
- < /head>
- < body>
- 代數(shù)和為負(fù)整數(shù)< h1>< s:property value="sum" />< /h1>
- < /body>
- < /html>
這兩個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來測試這個例子:
http://localhost:8080/struts2/sum.jsp
【編輯推薦】