該元素用於將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.
<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>
Eg2. 有input屬性的例子:
<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>
Eg3. 僅有JSP的action元素:
<action path="/menu" parameter="/default.jsp" type="org.apache.struts.actions.ForwardAction" />
首先,ActionServlet接到請求後調用ForwardAction的execute()方法,execute()根據配置的parameter屬性值來forward到那個URI。
這樣做的效果是:沒有任何form被實例化,比較現實的情形可能是form在request更高級別的範圍中定義;或者這個action被用作在應用程序編譯好後充當系統參數,只需要更改這個配置文件而不需要重新編譯系統。