Struts2設定檔,struts2設定檔詳解

來源:互聯網
上載者:User

Struts2設定檔,struts2設定檔詳解
簡介:

      與Struts2相關的設定檔有好幾個,常用的有 struts.properties , web.xml, struts.xml等。web.xml中配置Struts2的分發器Filter。struts.properties裡配置Struts2的一些屬性。struts.xml裡配置了Struts2的Action。Struts2的:http://struts.apache.org/

struts.properties

      struts.properties配置了Struts2的一些參數。每個可配的參數都有一個預設值。該檔案不是必須的,如果無需改變任何參數值,可以不用添加該檔案。

      常用的需要在struts.properties裡重新設定的屬性有:

//指定預設編碼集,對於請求參數帶有中文的情況應該設定陳GBK或GB2312.預設值UTF-8struts.i18n.encoding=GB2312//是否每次HTTP請求到達時,都重新載入國際化資源檔。預設值falsestruts.i18n.reload=true//但struts.xml改動後,是否重新載入該檔案。在開發階段建議將此屬性設定為“true”,提高開發效率。預設值falsestruts.configuration.xml.reload=true//是否使用Struts2的開發模式,可以獲得更多報錯資訊,便於調試。在開發階段設定為true。預設值falsestruts.devMode = true//設定瀏覽器是否緩衝靜態頁面。開發階段設定為false,以獲得伺服器的最新響應。預設值truestruts.serve.static.browserCache=true//指定尾碼為.action形式的請求可被Struts2處理。可配置多個請求尾碼,比如.do、.struts2等,配置時多個尾碼名用逗號隔開struts.action.extension=action,do,struts2,//設定管理員運行時的連接埠號碼。一般情況下該屬性不修改,如果連接埠號碼佔用則重新分配連接埠號碼。預設值80struts.url.http.port = 8080

 

       註:Struts2的預設屬性位於struts2-core-2.0.11.1.jar包org/apache/struts2下面的default.properties裡,使用者可以在項目的WEB-INF/class下添加struts.properties覆蓋預設的配置。

 web.xml

      任何MVC架構都需要與Web應用整合,這就不得不藉助於web.xml檔案,只有配置在web.xml檔案中的Servlet才會被載入。通常所有MVC架構都需要Web應用載入一個核心控制器,對於Struts2架構而言,需要載入StrutsPrepareAndExecuteFiter,該Filter將會載入應用的Struts2架構。

      除此之外,還可以在Web.xml中配置Struts2常量(struts.properties檔案專門用於配置常量)。

    配置StrutsPrepareAndExecuteFiter的代碼如下:

<filter>        <filter-name>struts2</filter-name>        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>    </filter>    <filter-mapping>        <filter-name>struts2</filter-name>        <url-pattern>/*</url-pattern>    </filter-mapping>

      註:以上程式碼片段存在於struts2-blank\WEB-INF中的web.xml

struts.xml 

      struts.xml是Struts2的核心設定檔,裡面配置Action, JSP, Exception, Intercepter等。另外,struts.properties裡的配置也可以配置在struts.xml中。

一個較為完整的struts.xml配置執行個體如下:

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"    "http://struts.apache.org/dtds/struts-2.3.dtd"><struts>     <!--           http://localhost:8081/hotelsys/test/demo.do            1. StrutsPrepareAndExecuteFilter根據struts.xml設定檔的資訊,確定凡是*.do結尾的請求,均歸struts處理。      2. 一旦發現.do結尾請求出現,直接攔截,做以下操作:         a. http://localhost:8081/hotelsys/test/demo (去除識別標誌)         b. /test/demo  (獲得真正訪問資源地址資訊)         c. /test獲得目錄,去匹配對應的包的namespace         d. demo 獲得action的名字,去對應包中尋找,如果找到,則建立該action的執行個體,調用其execute方法一次。         -->    <!-- 設定為開發模式,系統運行速度將降低,但是提供的出錯提示更加直觀,一般用於系統調試的時候使用 -->    <constant name="struts.devMode" value="true" />    <constant name="struts.action.extension" value="do"/>  <!--所有以*.do結尾的請求全部歸struts處理  ,如果沒有顯式指定action識別尾碼,預設就是action, 比如toInput.action-->    <constant name="struts.ui.theme" value="simple"></constant> <!-- struts2將自動幫程式員產生網頁,提供了多種產生模版 -->    <constant name="struts.i18n.encoding" value="utf-8"></constant>       <!--         package是若干個擁有相關功能的action的群組     -->    <package name="demoPak" namespace="/test" extends="struts-default">         <action name="demo" class="edu.fjnu.hotelsys.action.DemoAction">         </action>    </package>            <package name="hotelsys-default" namespace="/" extends="struts-default">            <interceptors>                <interceptor name="authentication" class="edu.fjnu.hotelsys.interceptor.AuthenticationInterceptor"></interceptor>                  <!-- 普通需要許可權的使用者操作   普通的,需要身分識別驗證才可以使用的action,將使用user攔截器stack-->         <interceptor-stack name="user">                <interceptor-ref name="authentication" />                <interceptor-ref name="defaultStack" />         </interceptor-stack>         <!-- 普通需要許可權的使用者表單提交        需要身分識別驗證才可以使用的action,同時該action中擁有表達提交操作,將使用user-submit攔截器              stack -->         <interceptor-stack name="user-submit">                <interceptor-ref name="user" />         </interceptor-stack>                  <!-- 需要身分識別驗證才可以使用的action,同時該action中擁有表達提交操作,同時該表單具備檔案提交功能,將使用user-file-submit攔截器
stack --> <interceptor-stack name="user-file-submit"> <interceptor-ref name="fileUpload"> <param name="allowedTypes">image/bmp,image/pjpeg,image/png</param> <param name="maximumSize">200000</param> </interceptor-ref> <interceptor-ref name="user"/> </interceptor-stack> <!-- 非許可權資訊訪問 沒有任何身份需求,都可以訪問的action,將使用guest攔截器stack--> <interceptor-stack name="guest"> <interceptor-ref name="defaultStack" /> </interceptor-stack> </interceptors> <!-- 沒有特殊指明攔截器的action,預設使用user攔截器stack --> <default-interceptor-ref name="user"/> <global-results> <result name="gotoLoginAction" type="redirectAction"> <param name="actionName">gotoLogin</param> <param name="namespace">/security</param> </result> <result name="error" type="redirect">/error.jsp</result> </global-results> </package> <include file="security-module.xml"/> <include file="room-module.xml"/> <include file="hotel-module.xml"/> <include file="user-module.xml"/> </struts>

 

      註:struts.xml設定檔的預設位置在struts2-blank包下/WEB-INF/calsses/struts.xml。可以struts.xml放到MyEclipse項目的src檔案夾下。並且,log4j2.xml也在此目錄下。

配置Struts2常數

配置Struts2常數有3中方式:

      1.在struts.properties檔案中配置常量。

      2.在web.xml檔案中配置核心Filter時通過初始化參數來配置常數。

      3.在struts.xml檔案中使用<constant.../>元素來配置常量。

通常,Struts2架構按如下搜尋順序來載入Struts2常量:

      1.struts-default.xml: 該檔案儲存在 struts2-core-2.3.1.2.jar 檔案中。

      2.struts-plugin.xml: 該檔案儲存在 struts2-xxx-2.3.1.2.jar檔案中。

      3.struts.xml: 該檔案是Web應用自己的struts2設定檔。

      4.struts.properties: 該檔案是Web應用預設的Struts2設定檔。

      5.web.xml: 該檔案是Web應用的設定檔。

      註:上面指定了Struts2架構搜尋Struts2常量的順序,但是如果在多個檔案中配置了同一個Struts2常量,則後面一個檔案中配置的常量值會覆蓋前面檔案中配置的常量值。

 

   


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.