struts2配置檔案中的include問題
今天遇到的一個這樣的問題:幾個struts2的配置檔案 用include 進struts.xml配置檔案裡面去。每個配置檔案都有 一個 相同的<global-results> 想把它們提出來來 放到struts.xml配置檔案裡面去。這樣就只需要配置一個<global-results> 了。但是遇到一點問題。404的。以下是網上找的解決方法。
(1)struts-user.xml
<struts> <package name="struts-user" extends="struts-default"> <global-results> <result type="redirect-action">UserAction_queryAll</result> </global-results> <action name="UserAction_login" class="userAction" method="login"></action> <action name="UserAction_insert" class="userAction" method="insert"></action> <action name="UserAction_update" class="userAction" method="update"></action> <action name="UserAction_delete" class="userAction" method="delete"></action> <action name="UserAction_queryById" class="userAction" method="queryById"></action> <action name="UserAction_queryByLike" class="userAction" method="queryByLike"></action> <action name="UserAction_queryAll" class="userAction" method="queryAll"> <result>/user/user_list.jsp</result> </action> </package> </struts>
(2)struts.xml(引入了struts-user.xml)
<struts>
<include file="struts-user.xml"></include>
<package name="struts" extends="struts-default"></package>
</struts>
1. 使用<include>標籤重用配置檔案
(1)在Struts2中提供了一個預設的struts.xml檔案,但如果package、action、interceptors等配置比較多時,都放到一個struts.xml檔案不太容易維護。因此,就需要將struts.xml檔案分成多個配置檔案,然後在struts.xml檔案中使用<include>標籤引用這些配置檔案。如上面的程式碼。
注意:用<include>引用的xml檔案也必須是完成的struts2的配置。實際上<include>在引用時是單獨解析的xml檔案,而不是將被引用的檔案插入到struts.xml檔案中。
注意:struts.xml和struts-user.xml中<package></package>標籤中的name屬性不能相同。道理很簡單,<struts></struts>標籤中可以有多個<package></package>標籤,要通過name屬性以示區別。
(2)Apache Struts 2 Documentation: Can we break up a large struts.xml file into smaller pieces --> Yes, there are two approaches. We can include other struts.xml file from a bootstrap, or we can package a struts.xml files in a JAR. Or both.
<1>By Include:A typical struts.xml files will have one or more include elements:
<struts>
<include file="struts-default.xml"/>
<include file="config-browser.xml"/>
<package name="default" extends="struts-default">
....
</package>
<include file="other.xml"/>
</struts>
The first include element tells the framework to load the struts-default.xml, which it will find in the struts2.jar file. The struts-default.xml file defines the "standard" interceptor and result definitions. You can put your own <include> elements in your
struts.xml interchangeably with <package> elements. The configuration objects will be loaded in the order of appearance. The framework reads the configuration from top to bottom and adds objects as they are referenced.
<2>By JAR
A "module" can be added to an application by placing a struts.xml and related classes into a JAR on the classpath. FreeMarker and Velocity templates can also be provided by JAR, making it possible to distribution a module in a single, self-contained JAR that is automatically configured on startup.
2. 全域性result(global-results)
(1)有很多時候一個<result>可供很多<action>使用,這時可以使用<global-results>標籤來定義全域性的<result>,程式碼見struts-user.xml。執行順序:當一個Action返回的String沒有相應的<result>與之對應,Struts2就會查詢全域性的<result>。
(2)Apache Struts 2 Documentation: Global Results
Most often, results are nested with the action element. But some results apply to multiple actions. In a secure application, a client might try to access a page without being authorized, and many actions may need access to a "logon" result. If actions need to share results, a set of global results can be defined for each package. The framework will first look for a local result nested in the action. If a local match is not found, then the global results are checked.
<!-- Defining global results -->
<global-results>
<result name="error">/Error.jsp</result>
<result name="invalid.token">/Error.jsp</result>
<result name="login" type="redirectAction">Logon!input</result>
</global-results>
3. <include>標籤和<global-results>標籤結合
(1)<global-results>標籤的作用域只是當前<struts></struts>,也可以說是當前的xml檔案;struts2不允許把struts-user.xml(通過<include>引入到struts.xml)中的<global-results>標籤寫在struts.xml中。
(2)如果struts-user.xml中的package繼承自struts.xml中的package,則可以將struts-user.xml中的<global-results>放在struts.xml中。然後struts-user.xml將此<global-results>從struts.xml中繼承過來。例如(將上面的兩段程式碼簡單修改):
(1)struts-user.xml
<struts>
<!-- 這裡struts-user繼承(extends)的是struts, 即struts.xml中package的name屬性值 -->
<package name="struts-user" extends="struts">
<action name="UserAction_login" class="userAction" method="login"></action>
<action name="UserAction_insert" class="userAction" method="insert"></action>
<action name="UserAction_update" class="userAction" method="update"></action>
<action name="UserAction_delete" class="userAction" method="delete"></action>
<action name="UserAction_queryById" class="userAction" method="queryById"></action>
<action name="UserAction_queryByLike" class="userAction" method="queryByLike"></action>
<action name="UserAction_queryAll" class="userAction" method="queryAll">
<result>/user/user_list.jsp</result>
</action>
</package>
</struts>
(2)struts.xml(引入了struts-user.xml)
<struts>
<include file="struts-user.xml"></include>
<package name="struts" extends="struts-default">
<global-results>
<result type="redirect-action">UserAction_queryAll</result>
</global-results>
</package>
</struts>