国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
Struts2的各種技術(shù)
在本系列教程中我們將學(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

本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
使用Struts的Token機(jī)制解決表單的重復(fù)提交
struts1 問題 報錯 集錦
Struts教程
mayong--淺析struts 體系結(jié)構(gòu)與工作原理
基于Struts的AJAX
Struts在Tomcat中的安裝配置及工作流程
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服