自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Struts2教程:第一個Struts2程序

開發(fā) 后端
本文為Struts2教程,本部分教你如何創(chuàng)建你的第一個Struts2程序。Struts2雖然在大版本號上是第二個版本,但基本上在配置和使用上已經(jīng)完全顛覆了Struts1.x的方式。

在本系列教程中我們將學(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的代碼:

  1. < filter> 
  2.     < filter-name>struts2< /filter-name> 
  3.     < filter-class> 
  4.         org.apache.struts2.dispatcher.FilterDispatcher              
  5.     < /filter-class> 
  6. < /filter> 
  7. < filter-mapping> 
  8.     < filter-name>struts2< /filter-name> 
  9.     < url-pattern>/*< /url-pattern> 
  10. < /filter-mapping> 

【第2步】 編寫Action類

    這一步和Struts1.x也必須進(jìn)行。只是Struts1.x中的動作類必須從Action類中繼承,而Struts2.x的動作類需要從com.opensymphony.xwork2.ActionSupport類繼承。下面是計算兩個整數(shù)代碼和的Action類,代碼如下:

  1. package action;  
  2.     
  3. import com.opensymphony.xwork2.ActionSupport;  
  4.     
  5. public class FirstAction extends ActionSupport  
  6. {  
  7.     private int operand1;  
  8.     private int operand2;  
  9.     
  10.     public String execute() throws Exception  
  11.     {  
  12.         if (getSum() >= 0)  // 如果代碼數(shù)和是非負(fù)整數(shù),跳到positive.jsp頁面  
  13.         {  
  14.             return "positive";  
  15.         }  
  16.         else  // 如果代碼數(shù)和是負(fù)整數(shù),跳到negative.jsp頁面  
  17.         {  
  18.             return "negative";  
  19.         }  
  20.     }  
  21.     
  22.     public int getOperand1()  
  23.     {  
  24.         return operand1;  
  25.     }  
  26.     
  27.     public void setOperand1(int operand1)  
  28.     {  
  29.         System.out.println(operand1);  
  30.           this.operand1 = operand1;  
  31.     }  
  32.     
  33.     public int getOperand2()  
  34.     {  
  35.         return operand2;  
  36.     }    
  37.     public void setOperand2(int operand2)  
  38.     {  
  39.         System.out.println(operand2);  
  40.         this.operand2 = operand2;  
  41.     }  
  42.     public int getSum()  
  43.     {  
  44.         return operand1 + operand2;  // 計算兩個整數(shù)的代碼數(shù)和  
  45.     }  

從上面的代碼可以看出,動作類的一個特征就是要覆蓋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中配置動作類的代碼:

  1. < ?xml version="1.0" encoding="UTF-8" ?> 
  2.   < !DOCTYPE struts PUBLIC  
  3.       "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
  4.       "http://struts.apache.org/dtds/struts-2.0.dtd"> 
  5.   < struts> 
  6.       < package name="struts2" namespace="/mystruts" 
  7.           extends="struts-default"> 
  8.           < action name="sum" class="action.FirstAction"> 
  9.               < result name="positive">/positive.jsp< /result> 
  10.               < result name="negative">/negative.jsp< /result> 
  11.           < /action> 
  12.       < /package> 
  13.   < /struts> 
  14.    

在< 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,代碼如下:

  1. < %@ page language="java" import="java.util.*" pageEncoding="GBK" %> 
  2.   < %@ taglib prefix="s" uri="/struts-tags"%> 
  3.     
  4.   < html> 
  5.       < head> 
  6.           < title>輸入操作數(shù)< /title> 
  7.       < /head> 
  8.     
  9.       < body> 
  10.            求代數(shù)和  
  11.           < br/> 
  12.           < s:form action="mystruts/sum.action" >                  
  13.               < s:textfield name="operand1" label=" 操作數(shù)1"/> 
  14.               < s:textfield name="operand2"  label=" 操作數(shù)2" />          
  15.               < s:submit value="代數(shù)和" />              
  16.           < /s:form> 
  17.       < /body> 
  18.   < /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

  1. < %@ page language="java" import="java.util.*" pageEncoding="GBK"%> 
  2.   < %@ taglib prefix="s" uri="/struts-tags" %> 
  3.     
  4.   < html> 
  5.     < head> 
  6.       < title>顯示代數(shù)和< /title> 
  7.     < /head> 
  8.       
  9.     < body>      
  10.       代數(shù)和為非負(fù)整數(shù)< h1>< s:property value="sum" />< /h1> 
  11.     < /body> 
  12.   < /html>  

3.       negative.jsp

  1. < %@ page language="java" import="java.util.*" pageEncoding="GBK"%> 
  2.  < %@ taglib prefix="s" uri="/struts-tags" %> 
  3.    
  4.  < html> 
  5.    < head> 
  6.      < title>顯示代數(shù)和< /title> 
  7.    < /head> 
  8.      
  9.    < body> 
  10.      代數(shù)和為負(fù)整數(shù)< h1>< s:property value="sum" />< /h1> 
  11.        
  12.    < /body> 
  13.  < /html>  

  
這兩個jsp頁面的實現(xiàn)代碼基本一樣,只使用了一個< s:property>標(biāo)簽來顯示Action類中的sum屬性值。< s:property>標(biāo)簽是從request對象中獲得了一個對象中得到的sum屬性,如我們可以使用如下的代碼來代替< s:property value=”sum”/>:

  1. < %  
  2.  
  3.  com.opensymphony.xwork2.util.OgnlValueStack ovs =   
  4.  
  5. (com.opensymphony.xwork2.util.OgnlValueStack)request.getAttribute("struts.valueStack");   
  6.  
  7.  out.println(ovs.findString("sum"));   
  8.  
  9. %> 

啟動Tomcat后,在IE中輸入如下的URL來測試這個例子:

http://localhost:8080/struts2/sum.jsp

【編輯推薦】

  1. Struts2中POI在內(nèi)存中生成文件并下載
  2. Struts2深入詳解properties配置文件
  3. Struts2 iterator介紹及功能詳解
  4. Struts2 checkbox適用場景及實例分析
  5. Struts2 國際化與防止刷新重復(fù)提交表單
責(zé)任編輯:yangsai 來源: BlogJava
相關(guān)推薦

2009-02-04 10:51:07

2009-02-04 11:00:44

2009-07-29 09:54:34

struts2和str

2009-06-25 15:22:03

Struts2教程一個form多個sub

2009-06-03 14:19:34

Struts2Guice

2009-06-25 16:04:30

2009-06-25 15:26:25

Struts2教程struts.xml常

2009-02-04 15:04:13

2009-06-25 15:54:42

Struts2教程攔截器

2009-06-25 15:50:03

Struts2教程上傳任意多個文件

2009-02-04 11:37:15

2009-07-03 09:35:57

Struts2 JSP

2009-06-04 08:34:24

Struts2配置struts.xml

2009-02-04 14:00:59

2009-06-25 15:33:12

Struts2教程使用validate驗證數(shù)據(jù)

2009-02-04 14:19:38

2009-06-25 15:37:12

Struts2教程Validation框

2011-08-19 13:13:14

struts2Java

2012-05-10 14:00:06

StrutsjsonJava

2013-07-19 09:36:04

struts2struts2漏洞
點贊
收藏

51CTO技術(shù)棧公眾號