標籤:
action-mappings
該元素用於將Action元素定義到ActionServlet類中,它含有0到多個<action/>元素,其格式如下:
<action-mappings>
<action path="Action請求的相對路徑,與頁面<html:form>的Action屬性值一致"
type="該Action的對應類的全路徑"
name="該Action綁定的FormBean,與<form-bean >的Name屬性值一致"
<forward name="與Action類中mapping.findForward("mapname")返回的mapname值一致" path="頁面跳轉的相對路徑"/>
</action>
</action-mappings>
每個action子項目可包含一個或多個forward子項目。除了path、type和name屬性外,action還具有如下屬性:
l scope:指定ActionForm Bean的範圍(session和request),預設為session。(可選);
l input:當Bean發生錯誤時返回的路徑,在validate驗證架構中錯誤顯示的頁面(可選);
l classname:指定一個調用這個Action類的ActionMapping類的全名。預設用org.apache.struts.action.ActionMapping(可選);
l include:如果沒有forward的時候,它起forward的作用(可選);
l validate:若為true,則會調用ActionForm的validate()方法或調用validate驗證,否則不調用,預設為true(可選)。
forward屬性也是可選的。
action元素定義舉例如下:
Example1.
Eg2. 有input屬性的例子:
- <action-mappings>
- <action
-
- path="/userAction"
-
- type="com.amigo.struts.action.UserAction"
-
- name="UserForm"
-
- scope="request"
-
- validate = "false"
-
- parameter="method" >
-
- <forward name="error" path="/user/error.jsp" />
-
- <forward name="success" path="/user/success.jsp"/>
-
- <forward name="add" path="/user/addUser.jsp"/>
-
- <forward name="update" path="/user/updateUser.jsp"/>
- <forward name="list" path="/user/userList.jsp"/>
-
- </action>
-
- </action-mappings></p>
Eg3. 僅有JSP的action元素:
- <action-mappings>
-
- <action path="/calcAction"
-
- type="com.amigo.struts.action.CalcAction"
-
- name="CalcForm"
-
- scope="request"
-
- validate="true"
-
- input="/index.jsp">
-
- <forward name="success" path="/success.jsp"/>
- <forward name="error" path="/error.jsp"/>
-
- </action>
-
- </action-mappings>
首先,ActionServlet接到請求後調用ForwardAction的execute()方法,execute()根據配置的parameter屬性值來forward到那個URI。
- <action path="/menu"
-
- parameter="/default.jsp"
-
- type="org.apache.struts.actions.ForwardAction" />
這樣做的效果是:沒有任何form被執行個體化,比較現實的情形可能是form在request更進階別的範圍中定義;或者這個action被用作在應用程式編譯好後充當系統參數,只需要更改這個設定檔而不需要重新編譯系統。
//轉自CSDN:http://blog.csdn.net/zhoukang0916/article/details/4210029
Struts-config.xml設定檔《action-mappings》元素的詳解