深入理解Struts2的設定檔

來源:互聯網
上載者:User
1.1.    包配置: Struts2架構中核心組件就是Action、攔截器等,Struts2架構使用包來管理Action和攔截器等。每個包就是多個Action、多個攔截器、多個攔截器引用的集合。 在struts.xml檔案中package元素用於定義包配置,每個package元素定義了一個包配置。它的常用屬性有: l name:必填屬性,用來指定包的名字。 l extends:可選屬性,用來指定該包繼承其他包。繼承其它包,可以繼承其它包中的Action定義、攔截器定義等。 l namespace:可選屬性,用來指定該包的命名空間。

<! DOCTYPE struts PUBLIC         "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"         "http://struts.apache.org/dtds/struts-2.0.dtd" > < struts >     <!-- struts2 的 action 必須放在一個指定的包空間下定義 -->     < package name = "default" extends = "struts-default" >     <!-- 定義處理請求 URL 為 login.action 的 Action -->         < action name = "login" class = "org.qiujy.web.struts.action.LoginAction" >         <!-- 定義處理結果字串和資源之間的映射關係 -->             < result name = "success" > /success.jsp </ result >             < result name = "error" > /error.jsp </ result >         </ action >     </ package > </ struts >
如上樣本的配置,配置了一個名為default的包,該包下定義了一個Action。 1.2.    命名空間配置: 考慮到同一個Web應用中需要同名的Action,Struts2以命名空間的方式來管理Action,同一個命名空間不能有同名的Action。 Struts2通過為包指定namespace屬性來為包下面的所有Action指定共同的命名空間。 把上樣本的配置改為如下形式:

<! DOCTYPE struts PUBLIC         "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"         "http://struts.apache.org/dtds/struts-2.0.dtd" > < struts >     <!-- struts2 的 action 必須放在一個指定的包空間下定義 -->     < package name = "qiujy" extends = "struts-default" >     <!-- 定義處理請求 URL 為 login.action 的 Action -->         < action name = "login" class = "org.qiujy.web.struts2.action.LoginAction" >         <!-- 定義處理結果字串和資源之間的映射關係 -->             < result name = "success" > /success.jsp </ result >             < result name = "error" > /error.jsp </ result >         </ action >     </ package >         < package name = "my" extends = "struts-default" namespace = "/manage" >     <!-- 定義處理請求 URL 為 login.action 的 Action -->         < action name = "backLogin" class = "org.qiujy.web.struts2.action.LoginAction" >         <!-- 定義處理結果字串和資源之間的映射關係 -->             < result name = "success" > /success.jsp </ result >             < result name = "error" > /error.jsp </ result >         </ action >     </ package > </ struts >
如上配置了兩個包:default和my,配置my包時指定了該包的命名空間為/manage。 對於包default:沒有指定namespace屬性。如果某個包沒有指定namespace屬性,即該包使用預設的命名空間,預設的命名空間總是""。 對於包my:指定了命名空間/manage,則該包下所有的Action處理的URL應該是“命名空間/Action名”。如上名為 backLogin的Action,它處理的URL為: http://localhost:8080/userlogin_struts2 /manage/backLogin.action Struts2的命名空間的作用等同於struts1裡模組的作用。 1.3.    包含配置: 在Struts2中可以將一個設定檔分解成多個設定檔,那麼我們必須在struts.xml中包含其他設定檔。

< struts >     < include file = "struts-default.xml" />     < include file = "struts-user.xml" />     < include file = "struts-book.xml" />     < include file = "struts-shoppingCart.xml" />         ......    </ struts >
1.4.    攔截器配置: 見後面章節介紹。 1.5.    常量配置: Struts2架構有兩個核心設定檔,其中struts.xml檔案主要負責管理應用中的Action映射, 及Action處理結果和實體資源之間的映射關係。除此之外,Struts2架構還包含了一個struts.properties檔案,該檔案主義了Struts2架構的大量常量屬性。但通常推薦也是在struts.xml檔案中來配置這些常量屬性。 如:後面會講到Struts2的國際化,它的資源檔位置就用常量屬性來指定:

< struts >     ......     < constant name = "struts.custom.i18n.resources" value = "messages" /> </ struts >
表示指定了資源檔的放置在classes目錄下,基本名是messages,則在classes目錄下您就應該放置類似messages_zh_CN.properties,message_en.properties名的檔案。 2.    Struts2的Action 2.1.    實現Action類: Struts2中Action是核心內容,它包含了對使用者請求的處理邏輯,我們也稱Action為業務控制器。 Struts2中的Action採用了低侵入式的設計,Struts2不要求Action類繼承任何的Struts2的基類或實現Struts2介面。(但是,我們為了方便實現Action,大多數情況下都會繼承com.opensymphony.xwork2.ActionSupport類,並重寫此類裡的public String execute() throws Exception方法。因為此類中實現了很多的實用介面,提供了很多預設方法,這些預設方法包括擷取國際化資訊的方法、資料校正的方法、預設的處理使用者請求的方法等,這樣可以大大的簡化Action的開發。) Struts2中通常直接使用Action來封裝HTTP請求參數,因此,Action類裡還應該包含與請求參數對應的屬性,並且為屬性提供對應的getter和setter方法。(當然,Action類中還可以封裝處理結果,把處理結果資訊當作一屬性,提供對應的getter和setter方法) 修改第一部分的使用者登入樣本:把Action改成如下:

package org.qiujy.web.struts2.action;   import com.opensymphony.xwork2.ActionSupport;   /**   * @author qiujy   * @version 1.0   */ publicclass LoginAction extends ActionSupport{     private String userName ;     private String password ;         private String

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.