struts2的開放02(struts.xml配置詳解)

<package name=“xasxt" namespace="/test" extends="struts-default">
 <action name="helloworld" class=“com.xasxt.action.HelloWorldAction">
  <result name="success">/WEB-INF/page/hello.jsp</result>
 </action>
 </package>
在struts2框架中使用包來管理Action,包的作用和java中的類包是非常類似的,它主要用於管理
一組業務功能相關的action。在實際應用中,我們應該把一組業務功能相關的Action放在同一個
包下。

配置包時必須指定name屬性,如果其他包要繼承該包,必須通過該屬性進行引用。包的namespace屬性
用於定義該包的命名空間。該屬性可以不配置,對本例而言,如果不指定該屬性,默認的命名空間爲“”
(空字符串)。

通常每個包都應該繼承struts-default包, struts-default包是由struts內置的,它定義了struts2內部的衆多攔截器和Result類型。Struts2很多核心的功能都是通過這些內置的攔截器實現的。如:從請求中把請求參數封裝到action、文件上傳和數據驗證等等都是通過攔截器實現的。當包繼承了struts-default包才能使用struts2提供的這些功能。 struts-default包是在struts2-core-2.x.x.jar文件中的struts-default.xml中定義。 struts-default.xml也是Struts2默認配置文件。 Struts2每次都會自動加載 struts-default.xml文件。

包還可以通過abstract=“true”定義爲抽象包,抽象包中不能包含action。

在struts1中,通過path屬性指定訪問該action的URL路徑。在struts2中,情況就不是這樣了,訪問struts2中的action的URL路徑由兩部份組成:包的命名空間+action的名稱,例如訪問上面例子中名爲helloworld的Action的URL路徑爲:/test/helloworld (注意:完整路徑爲:http://localhost:端口/內容路徑/test/helloworld.action)。

如果沒有爲action指定class,默認是ActionSupport。而ActionSupport的execute() 方法默認處理就是返回一個success字符串。method屬性用於指定action中的那個方法,如果沒有指定默認執行action中的execute() 方法。

Action名稱的搜索順序:

1.獲得請求路徑的URI,例如url是:http://server/struts2/path1/path2/path3/test.action
2.首先尋找namespace爲/path1/path2/path3的package,如果存在這個package,則在這個package中尋找名字爲test的action,如果不存在這個package則轉步驟3;
3.尋找namespace爲/path1/path2的package,如果存在這個package,則在這個package中尋找名字爲test的action,如果不存在這個package,則轉步驟4;
4.尋找namespace爲/path1的package,如果存在這個package,則在這個package中尋找名字爲test的action,如果仍然不存在這個package,就去默認的namaspace的package下面去找名字爲test的action,如果還是找不到,頁面提示找不到action。
在Struts.xml配置Action的result:

<action name="helloworld" class=“com.xasxt.action.HelloWorldAction">
 <result name="success">/WEB-INF/page/hello.jsp</result>
</action>
result配置類似於struts1中的forward,但struts2中提供了多種結果類型,如: dispatcher(默認值)、
 redirect 、 redirectAction 、 plainText。

下面是redirectAction 結果類型的例子,如果重定向的action中同一個包下:
<result type="redirectAction">helloworld</result>
如果重定向的action在別的命名空間下:
<result type="redirectAction">
 <param name="actionName">helloworld</param>
 <param name="namespace">/test</param>
</result>

如果沒有指定result的name屬性,默認值爲success。

在result中還可以使用${屬性名}表達式,表達式裏的屬性名對應action中的屬性。如下:
<result type="redirect">view.jsp?idD=${id}</result>

 指定需要Struts 2處理的請求後綴:

默認情況下,如果用戶請求的路徑以.action結尾,該請求將被轉入Struts 2框架處理,否則該請求將略過Struts 2框架的處理。

默認處理的後綴是可以通過常量”struts.action.extension“進行修改的,如下面配置Struts 2只處理以.do爲後綴的請求路徑:

<struts>
    <constant name="struts.action.extension" value="do"/>
</struts>

如果用戶需要指定多個請求後綴,則多個後綴之間以英文逗號(,)隔開。如:
  <constant name="struts.action.extension" value="do,go"/>

全局結果

當多個action中都使用到了相同result,這時我們應該把
result定義爲全局結果。struts1中提供了全局forward,
struts2中也提供了相似功能:
<package ....>
 <global-results>
  <result name="message">/message.jsp</result>
 </global-results>
</package>

 常用的常量

<!-- 指定默認編碼集,作用於HttpServletRequest的setCharacterEncoding方法 -->
    <constant name="struts.i18n.encoding" value="UTF-8"/>
    <!-- 該屬性指定需要Struts 2處理的請求後綴,如果用戶需要指定多個請求後綴,則多個後綴之間以英文逗號(,)隔開。 -->
    <constant name="struts.action.extension" value="do"/>
    <!-- 設置瀏覽器是否緩存靜態內容,默認值爲true(生產環境下使用),開發階段最好關閉 -->
      <constant name="struts.serve.static.browserCache" value="false"/>
    <!-- 當struts的配置文件修改後,系統是否自動重新加載該文件,默認值爲false(生產環境下使用),開發階段最好打開 -->
    <constant name="struts.configuration.xml.reload" value="true"/>
    <!-- 開發模式下使用,這樣可以打印出更詳細的錯誤信息 -->
    <constant name="struts.devMode" value="true" />
     <!-- 默認的視圖主題 -->
    <constant name="struts.ui.theme" value="simple" />
    <!– 與spring集成時,指定由spring負責action對象的創建 -->
    <constant name="struts.objectFactory" value="spring" />
 <!–該屬性設置Struts 2是否支持動態方法調用,該屬性的默認值是true。如果需要關閉動態方法調用,則可設置該屬性爲false。 -->
<constant name="struts.enable.DynamicMethodInvocation" value="false"/>


 


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章