struts2配置說明:
2010
-
12
-
31
16
:
38
:
54
| 分類: struts2 | 標籤: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中配置的。該屬性指定Struts
2
框架默認加載的配置文件,如果需要指定默認加載多個配置文件,則多個配置文件的文件名之間以英文逗號(,)隔開。該屬性的默認值爲struts-
default
.xml,struts-plugin.xml,struts.xml,這就是上圖中加載的三個配置文件。
Struts2常量的具體用法實例
Xml代碼
<?xml version=
"1.0"
encoding=
"UTF-8"
?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
<struts>
<!-- 指定Web應用的默認編碼集,相當於調用HttpServletRequest的setCharacterEncoding方法 -->
<constant name=
"struts.i18n.encoding"
value=
"UTF-8"
/>
<!--
該屬性指定需要Struts
2
處理的請求後綴,該屬性的默認值是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, and constructor (name
是默認裝配模式)
-->
<constant name=
"struts.objectFactory.spring.autoWire"
value=
"name"
/>
<!-- 該屬性指定整合spring時,是否對bean進行緩存,值爲
true
or
false
,默認爲
true
-->
<cosntant name=
"struts.objectFactory.spring.useClassCache"
/>
<!-- 指定類型檢查,包含tiger和notiger -->
<cosntant name=
"struts.objectTypeDeterminer"
value=
"tiger"
/>
<!-- 該屬性指定處理 MIME-type multipart/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"
/>
<!-- 該屬性指定Struts
2
文件上傳中整個請求內容允許的最大字節數 -->
<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文件改動時,是否重新加載 -->
<cosntant name=
"struts.configuration.xml.reload"
value=
"true"
/>
<!-- 設置struts是否爲開發模式,默認爲
false
,測試階段一般設爲
true
.
-->
<cosntant name=
"struts.devMode"
value=
"true"
/>
<!-- 設置是否每次請求,都重新加載資源文件,默認值爲
false
. -->
<cosntant name=
"struts.i18n.reload"
value=
"false"
/>
<!-- 標準的UI主題,默認的UI主題爲xhtml,可以爲simple,xhtml或ajax -->
<cosntant name=
"struts.ui.theme"
value=
"xhtml"
/>
<!-- 模板目錄 -->
<cosntant name=
"struts.ui.templateDir"
value=
"template"
/>
<!-- 設置模板類型. 可以爲 ftl, vm, or jsp -->
<cosntant name=
"struts.ui.templateSuffix"
value=
"ftl"
/>
<!-- 定位velocity.properties 文件. 默認velocity.properties -->
<cosntant name=
"struts.velocity.configfile"
value=
"velocity.properties"
/>
<!-- 設置velocity的context. -->
<cosntant name=
"struts.velocity.contexts"
value=
"...."
/>
<!-- 定位toolbox -->
<cosntant name=
"struts.velocity.toolboxlocation"
value=
"...."
/>
<!-- 指定web應用的端口 -->
<cosntant name=
"struts.url.http.port"
value=
"80"
/>
<!-- 指定加密端口 -->
<cosntant name=
"struts.url.https.port"
value=
"443"
/>
<!-- 設置生成url時,是否包含參數.值可以爲: none,get or all -->
<cosntant name=
"struts.url.includeParams"
value=
"get"
/>
<!-- 設置要加載的國際化資源文件,以逗號分隔. -->
<cosntant name=
"struts.custom.i18n.resources"
value=
"application"
/>
<!-- 對於一些web應用服務器不能處理HttpServletRequest.getParameterMap(),
像 WebLogic,Orion, and OC4J等,須設置成
true
,默認爲
false
.
-->
<cosntant name=
"struts.dispatcher.parametersWorkaround"
value=
"false"
/>
<!-- 指定freemarker管理器 -->
<cosntant name=
"struts.freemarker.manager.classname"
value=
"org.apache.struts2.views.freemarker.FreemarkerManager"
/>
<!-- 設置是否對freemarker的模板設置緩存,效果相當於把template拷貝到 WEB_APP/templates. -->
<cosntant name=
"struts.freemarker.templatesCache"
value=
"false"
/>
<!-- 通常不需要修改此屬性. -->
<cosntant name=
"struts.freemarker.wrapper.altMap"
value=
"true"
/>
<!-- 指定xslt result是否使用樣式表緩存.開發階段設爲
true
,發佈階段設爲
false
.
-->
<cosntant name=
"struts.xslt.nocache"
value=
"false"
/>
<!-- 設置struts自動加載的文件列表. -->
<cosntant name=
"struts.configuration.files"
value=
"struts-default.xml,struts-plugin.xml,struts.xml"
/>
<!-- 設定是否一直在最後一個slash之前的任何位置選定namespace. -->
<cosntant name=
"struts.mapper.alwaysSelectFullNamespace"
value=
"false"
/>
</struts>
<struts>
<!-- include節點是struts2中組件化的方式 可以將每個功能模塊獨立到一個xml配置文件中 然後用include節點引用 -->
<include file=
"struts-default.xml"
></include>
<!--
package
提供了將多個Action組織爲一個模塊的方式
package
的名字必須是唯一的
package
可以擴展 當一個
package
擴展自
另一個
package
時該
package
會在本身配置的基礎上加入擴展的
package
的配置 父
package
必須在子
package
前配置
name:
package
名稱
extends
:繼承的父
package
名稱
abstract
:設置
package
的屬性爲抽象的 抽象的
package
不能定義action
值
true
:
false
namespace:定義
package
命名空間 該命名空間影響到url的地址,例如此命名空間爲/test那麼訪問是的地址爲http:
-->
<
package
name=
"com.kay.struts2"
extends
=
"struts-default"
namespace=
"/test"
>
<interceptors>
<!-- 定義攔截器
name:攔截器名稱
class
:攔截器類路徑
-->
<interceptor name=
"timer"
class
=
"com.kay.timer"
></interceptor>
<interceptor name=
"logger"
class
=
"com.kay.logger"
></interceptor>
<!-- 定義攔截器棧 -->
<interceptor-stack name=
"mystack"
>
<interceptor-ref name=
"timer"
></interceptor-ref>
<interceptor-ref name=
"logger"
></interceptor-ref>
</interceptor-stack>
</interceptors>
<!-- 定義默認的攔截器 每個Action都會自動引用
如果Action中引用了其它的攔截器 默認的攔截器將無效 -->
<
default
-interceptor-ref name=
"mystack"
></
default
-interceptor-ref>
<!-- 全局results配置 -->
<global-results>
<result name=
"input"
>/error.jsp</result>
</global-results>
<!-- Action配置 一個Action可以被多次映射(只要action配置中的name不同)
name:action名稱
class
: 對應的類的路徑
method: 調用Action中的方法名
-->
<action name=
"hello"
class
=
"com.kay.struts2.Action.LoginAction"
>
<!-- 引用攔截器
name:攔截器名稱或攔截器棧名稱
-->
<interceptor-ref name=
"timer"
></interceptor-ref>
<!-- 節點配置
name : result名稱 和Action中返回的值相同
type : result類型 不寫則選用superpackage的type struts-
default
.xml中的默認爲dispatcher
-->
<result name=
"success"
type=
"dispatcher"
>/talk.jsp</result>
<!-- 參數設置
name:對應Action中的get/set方法
-->
<param name=
"url"
>http:
</action>
</
package
>
</struts>
二、Struts2配置元素說明<轉載>
Struts2核心的配置文件是缺省的struts.xml。
必要的時候,缺省的配置文件可以包含其它的配置文件;struts文件可以放入jar中,並自動插入應用程序,這樣每個模塊可以包含自己的配置文件並自動配置。在Freemarker和Velocity模塊中,模板也能從classpath中加載,所以整個模塊可以作爲一個簡單的jar文件被包含。
Struts.xml配置文件可以包含Interceptor、Action類和Results。
Struts.xml配置元素說明:
1
、Packages
Packages:packages把actions、results、results types、interceptors
和interceptor-stacks組裝到一個邏輯單元中,從概念上講,packages就像一個對象,可以被其它子包從寫,而且可以擁有自己獨立的部分。
Name屬性是packages的必填元素,它作爲一個關鍵字被後邊的包引用;
extends
元素是可選的,它允許包擴展一個和多個前邊定義的包。注意,
struts.xml文件是至上而下處理的,所有被擴展的包,需要在擴展包前定義。
Abstract元素是可選的,它可以申明一個不包含actions的配置文件。
2
、Namespace
Namespace元素把actions細分到邏輯模塊,每一個namespace都有自己的
前綴(prefix),namespace避免了action之間的名字衝突,當前綴出現在URI中時,這些標籤都是名字空間感知的(“namespace aware”),所以這些namespace prefix不必嵌入到表單或連接中。
Default的namespace是一個空字符串“”,如果在指定的配置文件中,沒有找到action,缺省的namespace也會被查找。Local/global策略允許應用程序在action “
extends
”元素層次結構之外,有全局的action配置。缺省的namespace也可以不在
package
中申明。
Namespace prefix可以註冊爲java的申明式安全,以確保授權的用戶才能訪問namespace的資源。
Root namespace(“/”)也被支持,root就是當直接請求context path的時候的namespace。
[個人理解:namespace的用法就像struts1.x中的path一樣,只不過它在
package
中什麼路徑,而在action中申明action名子罷了]
3
、Include
Include元素使得框架能應用“divide and conquer”來配置文件。被include
的每個配置文件必須和struts.xml有一樣的格式,一個大的項目可以採用這樣方式來組織程序模塊。
Include元素也可以和
package
交替出現,框架將按照順序加載配置文件。
4
、Interceptor configuration
Interceptor允許應用程序在Action方法執行前後定義執行代碼,
Interceptor在應用程序開發中十分重要,對於Interceptor有許多用例,如validation, property population, security, logging, 和profiling。
Interceptor被定義爲一個Java類,Interceptor也可以組裝成Interceptor-stack,他們將按照定義的順序執行。
在struts-
default
.xml中定義了一些缺省的Interceptor-stack,以便框架能很好地運行。
5
、Action
Action是框架的“工作單元”。Action可以指定一個Interceptor-stack、
一序列的
return
type和一序列的異常處理,但只有name屬性是必須的。
一:配置struts2。
首先在web.xml文件中配置filter
Xml代碼
<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>*.action</url-pattern>
</filter-mapping>
<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>*.action</url-pattern>
</filter-mapping>
然後在classpath中創建struts.xml配置文件。
Xml代碼
<?xml version=
"1.0"
encoding=
"UTF-8"
?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
<struts>
<constant name=
"struts.devMode"
value=http:
<constant name=
"struts.convention.default.parent.package"
value=http:
<constant name=
"struts.convention.package.locators"
value=http:
<
package
name=
"default-package"
extends
=
"convention-default"
>
<
default
-action-ref name=
"index"
/>
<action name=
"index"
>
<result>/WEB-INF/content/index.jsp</result>
</action>
</
package
>
</struts>
<?xml version=
"1.0"
encoding=
"UTF-8"
?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
<struts>
<constant name=
"struts.devMode"
value=http:
<constant name=
"struts.convention.default.parent.package"
value=http:
<constant name=
"struts.convention.package.locators"
value=http:
<
package
name=
"default-package"
extends
=
"convention-default"
>
<
default
-action-ref name=
"index"
/>
<action name=
"index"
>
<result>/WEB-INF/content/index.jsp</result>
</action>
</
package
>
</struts>
struts.devMode屬性,配置啓用調試,將有更多的錯誤信息輸出,便於排錯。struts.convention.
default
.parent.
package
屬性,指定使用註解標
注的控制器的默認包。可以在這個默認包中配置全局信息。
struts.convention.
package
.locators屬性,爲查找控制器包路徑的關鍵字。如com.mycompany.action,com.mycompany.action.user,都會被
struts2掃描。裏面有繼承至Action的類,或類名以Action結尾的類,都會做爲Action處理。
<
default
-action-ref name=
"index"
/>指定了默認action,如果指定的action不存在則訪問該action。
把struts2-spring-plugin-
2.1
.
6
.jar添加到classpath中,struts2會自動掃描struts-plugin.xml文件,該文件自動註冊了
com.opensymphony.xwork2.ObjectFactory,完成和spring的集成。