在 struts.xml 配置文件中:apache
<!-- package: 包。struts2 用它來組織模塊。jsp
name 屬性:必須,用於其餘的包引用當前的包.spa
extends 屬性:當前包繼承哪一個包。繼承,便可以繼承其中的全部的配置。一般狀況下繼承 struts-default ,這個包在 struts-default.xml 文件中定義。xml
namespace 屬性:可選。若是沒有給出,則默認爲 / 。若 namespace 有一個非默認值,則要想調用這個包裏的 Action,就必須把這個屬性所定義的命名空間添加到有關的 URI 字符串裏。例如: http://localhost:8080/contextPath/namespace/actionName.action-->繼承
<package name="helloWorld" extends="struts-default">字符串
<!-- 配置一個 action:一個struts2 的請求就是一個 actioninput
name:對應一個 Struts2 的請求的名字(或對應一個 servletPath,但去除 / 和 擴展名),但不包含擴展名。servlet
class:的默認值爲:com.opensymphony.xwork2.ActionSupportio
method:的默認值爲:executeclass
result:結果-->
<action name="input" class="com.opensymphony.xwork2.ActionSupport" method="execute">
<!--result:結果。表示 action 方法執行後可能返回一個結果。因此一個 action 節點可能會有多個 result 子節點,多個 result 子節點使用 name 來區分。
name:標識一個 result,和 action方法的返回值對應,默認值爲 success。
type:表示結果的類型。默認值爲 diapatcher(轉發到結果)-->
<result name="success" type="dispatcher">/WEB-INF/page/input.jsp</result>
</action>
</package>
關於 Strutts2 請求的擴展名:
在 org.apache.struts2 包下的 default.properties 中配置了 Struts2 應用的一些常量。
配置 Struts 能夠受理的請求 的擴展名,在 struts.xml 文件中以常量配置的方式修改 default.properties 所配置的常量。
例如:<constant name="struts.action.extension" value="action,do"></constant>
ActionSupport:
1)、ActionSupport 是默認的 Action 類,若某個 action 節點沒有配置 class 屬性,則 ActionSupport 即爲待執行的 Action 類。 而 execute 方法即爲要默認執行的 action 方法。
2)、在手工完成字段驗證,顯示錯誤消息,國際化等狀況下,推薦繼承 ActionSupport 。
result:
result 表明 action 方法執行後,可能去的一個目的地。
一個 action 節點,能夠配置多個 result 子節點。
result 的 name 屬性值對應值 action方法可能有的一個返回值。
result 一共有兩個屬性,還有一個是 type:表示結果的響應類型。
--result 的 type 屬性值,在struts-default 包的 result-type 節點的 name 屬性中定義。
經常使用的有:
> dispatcher(默認的):轉發。同 Servlet 中的轉發。
例:<result name="a" type="dispatcher">/a.jsp</result>
> redirect:重定向。
例:<result name="b" type="redirect">/b.jsp</result>
> redirectAction:重定向到一個Action。注意:經過 redirect 也能夠便捷的實現 redirectAction 的功能。
例:<result name="c" type="redirectAction">
<param name="actionName">testAction</param>
<param name="namespace">/hnust</param>
</result>
還須要再寫一個 packa:
<package name="test" namespace="/hnust" extends="struts-default">
<action name="testAction" class="com.hnust.struts2.action.TestAction">
<result>/success.jsp</result>
</action>
</package>
> chain:轉發到一個 Action。注意:不能經過 diapatcher 的方式轉發到一個 Action。
例:<result name="d" type="chain">
<param name="actionName">testAction</param>
<param name="namespace">/hnust</param>
</result>