struts2常量配置詳解以及簡單理解流程

一、常量配置詳解

通常struts2加載struts2常量的順序如下:
1.struts-default.xml:該文件保存在struts2-core-2.0.6.jar文件中。
2.struts-plugin.xml:該文件保存在struts2-Xxx-2.0.6.jar等Struts2插件JAR文件中。
3.struts.xml:該文件是Web應用默認的Struts2配置文件。
4.struts.properties:該文件是Web應用默認的Struts2配置文件。
5.web.xml:該文件是Web應用的配置文件。

如果在多個文件中配置了同一個Struts2常量,則後一個文件中的配置的常量值會覆蓋前面文件中配置的常量值。
在不同文件中配置常量的方式是不一樣的,但不管哪個文件中,配置Struts2常量都要指定兩個屬性:常量name和常量value。

推薦在struts.xml文件中配置Struts2常量。

此處只加載了前三個配置文件,這是在常量struts.configuration.files中配置的。該屬性指定Struts2框架默認加載的配置文件,如果需要指定默認加載多個配置文件,則多個配置文件的文件名之間以英文逗號(,)隔開。該屬性的默認值爲struts-default.xml,struts-plugin.xml,struts.xml,這就是上圖中加載的三個配置文件。

Struts2常量的具體用法實例
Xml代碼

<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEstrutsPUBLIC
"-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
<!--指定Web應用的默認編碼集,相當於調用HttpServletRequest的setCharacterEncoding方法-->
<constant name="struts.i18n.encoding"  value="UTF-8"/>

<!--
該屬性指定需要Struts2處理的請求後綴,該屬性的默認值是action,即所有匹配*.action的請求都由Struts2處理。
如果用戶需要指定多個請求後綴,則多個後綴之間以英文逗號(,)隔開。
-->
<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託管-->
<constant  name="struts.objectFactory"  value="spring"/>

<!--
指定加載struts2配置文件管理器,默認爲org.apache.struts2.config.DefaultConfiguration
開發者可以自定義配置文件管理器,該類要實現Configuration接口,可以自動加載struts2配置文件。
-->
<constant  name="struts.configuration"
value="org.apache.struts2.config.DefaultConfiguration"/>

<!--設置默認的locale和字符編碼-->
<constant  name="struts.locale"  value="zh_CN"/>
<constant  name="struts.i18n.encoding"  value="GBK"/>

<!--指定Struts的工廠類-->
<constant name="struts.objectFactory" value="spring"></constant>

<!--
指定spring框架的裝配模式,裝配方式有:name,type,auto,andconstructor(name
是默認裝配模式)
-->
<constant name="struts.objectFactory.spring.autoWire" value="name"/>

<!--該屬性指定整合spring時,是否對bean進行緩存,值爲trueor/false,默認爲true-->
<constant name="struts.objectFactory.spring.useClassCache"/>

<!--指定類型檢查,包含tiger和notiger-->
<constant name="struts.objectTypeDeterminer"value="tiger"/>

<!--該屬性指定處理MIME-typemultipart/form-data,文件上傳-->
<constant name="struts.multipart.parser" value="cos"/>
<constant name="struts.multipart.parser" value="pell"/>
<constant name="struts.multipart.parser" value="jakarta"/>

<!--指定上傳文件時的臨時目錄,默認使用javax.servlet.context.tempdir-->
<constant name="struts.multipart.saveDir" value="/tmpuploadfiles"/>

<!--該屬性指定Struts2文件上傳中整個請求內容允許的最大字節數-->
<constant name="struts.multipart.maxSize" value="2097152"/>

<!--
該屬性指定Struts2應用加載用戶自定義的屬性文件,該自定義屬性文件指定的屬性不會覆蓋
struts.properties文件中指定的屬性。如果需要加載多個自定義屬性文件,多個自定義屬性文
件的文件名以英文逗號(,)隔開。(也就是說不要改寫struts.properties!)
-->
<constant name="struts.custom.properties"
value="application,org/apache/struts2/extension/custom"/>

<!--指定請求url與action映射器,默認爲org.apache.struts2.dispatcher.mapper.DefaultActionMapper-->
<constant  name="struts.mapper.class" value="org.apache.struts2.dispatcher.mapper.DefaultActionMapper"/>

<!--指定action的後綴,默認爲action-->
<constant  name="struts.action.extension" value="do"/>

<!--被FilterDispatcher使用指定瀏覽器是否緩存靜態內容,測試階段設置爲false,發佈階段設置爲true.-->
<constant name="struts.serve.static.browserCache "value="true"/>

<!--設置是否支持動態方法調用,true爲支持,false不支持.-->
<constant name="struts.enable.DynamicMethodInvocation" value="true"/>

<!--設置是否可以在action中使用斜線,默認爲false不可以,想使用需設置爲true.-->
<constant name="struts.enable.SlashesInActionNames"  value="true"/>

<!--是否允許使用表達式語法,默認爲true.-->
<constant name="struts.tag.altSyntax" value="true"/>

<!--設置當struts.xml文件改動時,是否重新加載-->
<constant name="struts.configuration.xml.reload" value="true"/>

<!--設置struts是否爲開發模式,默認爲false,測試階段一般設爲true.-->
<constant name="struts.devMode" value="true"/>

<!--設置是否每次請求,都重新加載資源文件,默認值爲false.-->
<constant name="struts.i18n.reload" value="false"/>

<!--標準的UI主題,默認的UI主題爲xhtml,可以爲simple,xhtml或ajax-->
<constant name="struts.ui.theme" value="xhtml"/>

<!--模板目錄-->
<constant name="struts.ui.templateDir" value="template"/>

<!--設置模板類型.可以爲ftl,vm,orjsp-->
<constant name="struts.ui.templateSuffix" value="ftl"/>

<!--定位velocity.properties文件.默認velocity.properties-->
<constant name="struts.velocity.configfile" value="velocity.properties"/>

<!--設置velocity的context.-->
<constant name="struts.velocity.contexts" value="...."/>

<!--定位toolbox-->
<constant name="struts.velocity.toolboxlocation" value="...."/>

<!--指定web應用的端口-->
<constant name="struts.url.http.port" value="80"/>

<!--指定加密端口-->
<constant name="struts.url.https.port" value="443"/>

<!--設置生成url時,是否包含參數.值可以爲:none,getorall-->
<constant name="struts.url.includeParams" value="get"/>

<!--設置要加載的國際化資源文件,以逗號分隔.-->
<constant name="struts.custom.i18n.resources" value="application"/>

<!--對於一些web應用服務器不能處理HttpServletRequest.getParameterMap(),
像WebLogic,Orion,andOC4J等,須設置成true,默認爲false.-->
<cosntantname="struts.dispatcher.parametersWorkaround"value="false"/>

<!--指定freemarker管理器-->
<constant name="struts.freemarker.manager.classname" value="org.apache.struts2.views.freemarker.FreemarkerManager"/>

<!--設置是否對freemarker的模板設置緩存,效果相當於把template拷貝到WEB_APP/templates.-->
<cosntantname="struts.freemarker.templatesCache"value="false"/>

<!--通常不需要修改此屬性.-->
<constant name="struts.freemarker.wrapper.altMap" value="true"/>

<!--指定xsltresult是否使用樣式表緩存.開發階段設爲true,發佈階段設爲false.-->
<cosntantname="struts.xslt.nocache"value="false"/>
<!--設置struts自動加載的文件列表.-->
<constant name="struts.configuration.files" value="struts-default.xml,struts-plugin.xml,struts.xml"/>

<!--設定是否一直在最後一個slash之前的任何位置選定namespace.-->
<constant name="struts.mapper.alwaysSelectFullNamespace" value="false"/>
</struts>

1、在struts2中,表單中提交的action,爲什麼一定要用xxx.action的形式呢?原因是在default.properties這個資源文件(只讀)中是這麼定義的。可以去修改它裏面的配置。(方法是:在src目錄下創建struts.properties文件,然後修改成自己需要的形式即可。),另外,爲什麼struts2的配置文件名一定要是struts.xml呢?其實這些定義都是在default.properties文件(只讀)中配置好的。當然可以找到它,進行修改。(方法是:在src目錄下創建struts.properties文件,然後修改成自己需要的形式即可。原因是)

2、如何防止表單中提交的中文,取出後顯示是亂碼:解決方法:

只需要在struts.xml文件的標籤間加入一句代碼即可

<struts>

<constantname=”struts.i18n.encoding”value=”GBK”></constant>

</struts>

當然,也可以在struts.properties或web.xml中進行轉碼的配置。但是優先級的順序是:web.xml>struts.properties>struts.xml

在struts.properties(位於src目錄下)中配置轉碼的格式:

struts.i18n.encoding=GBK

在web.xml中配置轉碼的代碼是:在中增加如下代碼:

<filter>

<init-param>

<param-name>struts.i18n.encoding</param-name>

<param-value>GBK</param-value>

</init-param>

</filter>

3、要測試一個action運行的時間(會在控制檯輸出執行時間)。只需要struts.xml文件中的標籤中加入如一代碼(攔截器,具體說明與使用需要查看struts-default.xml文件),並在需要測試的action標籤內部加上對它的引用即可。

<package>
<interceptors>  <interceptorname=”timer”class=”com.opensymphony.xwork2.interceptor.TimerInterceptor”>

</<interceptorname=”params”class=”om.opensymphony.xwork2.interceptor.ParametersIncerceptor”>//爲獲取表單的參數需要加入這一句

<interceptor>


</interceptor>
</interceptors>

</package>

下面是對上面代碼的引用:

<action>

<interceptor-refname=”timer”/>

<interceptor-refname=”params”/>//引用一下獲取表單參數

</action>

也就是說:如果加上了timer這個屬性,就得加上params這個參數來獲取表單中的參數。有點類似構造方法的原理

4、struts.xml文件中標籤中的method屬性,來指定該action調用method屬性的值的那個方法。方法可以定義在xxxAction.java文件中。如:struts.xml中配置如下

<actionname="modify"class="com.test.action.TestMethod"method="modify">

<resultname="success">/resultmodify.jsp</result>

    </action>

    //表示modify這個方法存在於TestMethod.java這個類中。只要瀏覽器一請求modify.action,就會自動轉到相應的類中並調用相應的mofidy()。當然,<action></action>標籤中的name屬性名是可以隨意取的。只是在請求時名稱要與它一致即可。                

    <actionname="query"class="com.test.action.TestMethod"method="query">

        <resultname="success">/resultquery.jsp</result>

    </action>

//與上面的功能類似

5、在action標籤中用通配符映射來匹配:

  • 表示0-N個字符,不包括/ 常用

** 表示0-N個字符,包括/

\ 表示轉義符

比如:將上面兩個action標籤合併爲一個action標籤。如下:

<action name="*User" class="com.test.action.TestMethod" method="{1}">

<result name="success">/{1}UserSucc.jsp</result>

    </action>

它可以表示任何對User進行操作的acion。都由TestMethod這個類來處理。並且method屬性的值,是隨着訪問的變化而進行變化調用類中相應的方法。跳轉到的頁面也一樣的規律。這種方式,要求取的方法名要有規律。

沒有任何通配符的action匹配優先級最高。有通配符時,一律按順序匹配。

二、struts2流程

這裏寫圖片描述

三、struts2與struts和springMVC的區別

這裏寫圖片描述

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