struts2-核心配置檔案
阿新 • • 發佈:2018-12-06
簡單的struts.xml檔案內容如下,這是struts2的核心配置檔案
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<package name="test" namespace="/test" extends ="struts-default">
<action name="Test" >
<result>/test.jsp</result>
</action>
<action name="login" class="com.test.loginAction">
<result name="success">/login.jsp</result>
<result name ="fail">/index.jsp</result>
</action>
</package>
</struts>
< package>< /package>:
name屬性可以起任意內容,但一般與namespace有對應關係;
namespace屬性必須以 “/”開頭(代表當前應用根目錄);
extends一般繼承abstract=”true”的package,這裡的struts-default定義在struts-default.xml配置裡;
當package中沒有action
< action>< /action>:
name屬性就是定義的action名;
class屬性需要填寫相關action類的全限定性類名(如果不寫class則是com.opensymphony.xwork2.ActionSupport);
method屬性中可自定義action執行方法(預設是execute)
< result>< /result>:
name屬性為action方法執行後的返回值(預設不寫為success);
type屬性為result的後續行為(dispatcher(預設),redirect,redirectAction,chain)
redirect兩種寫法:
<!-- <result type="redirect">/login.jsp?user=${user}&password=${password}</result> -->
<result type="redirect">
<param name="Location">/login.jsp</param>
<param name="user">${user}</param>
<param name="password">${password}</param>
</result>
redirectAction:
<action name="action_1" class="com.sblov.action_1">
<result type="redirectAction">
<param name="actionName">action_2</param>
<param name="user">${user}</param>
<param name="password">${password}</param>
</result>
</action>
<action name="action_2" class="com.sblov.action_2">
<result>/login.jsp</result>
</action>
chain:
<action name="action_1" class="com.sblov.action_1">
<result type="chain">action_2</result>
</action>
<action name="action_2" class="com.sblov.action_2">
<result>/login.jsp</result>
</action>
package下的global-results(包範圍全域性檢視):可以減少多個action訪問相同頁面的冗餘程式碼
<global-results>
<result name="a">/a.jsp</result>
<result name="b">/b.jsp</result>
</global-results>
<action name="action_1" class="com.sblov.action_1">
<result >/success.jsp</result>
<!-- <result name="a">/a.jsp</result> -->
<!-- <result name="b">/b.jsp</result> -->
</action>
<action name="action_2" class="com.sblov.action_2">
<result>/login.jsp</result>
<!-- <result name="a">/a.jsp</result> -->
<!-- <result name="b">/b.jsp</result> -->
</action>
應用範圍全域性檢視:將global-results單獨放於一個package,其他包可繼承此包
指定多個配置檔案:< struts>< /struts>下用include
<include file="struts-1.xml" />
<include file="struts-2.xml" />
<include file="struts-*.xml" /><!-- 可用正則表示式匹配,但不能包含本檔案 -->