Struts-config.xml配置文件《action-mappings》元素的詳解

 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子元素。除了pathtypename屬性外,action還具有如下屬性:

l         scope:指定ActionForm Bean的作用域(sessionrequest),缺省爲session(可選)

l         input:當Bean發生錯誤時返回的路徑,在validate驗證框架中錯誤顯示的頁面(可選)

l         classname:指定一個調用這個Action類的ActionMapping類的全名。缺省用org.apache.struts.action.ActionMapping(可選)

l         include:如果沒有forward的時候,它起forward的作用(可選)

l         validate:若爲true,則會調用ActionFormvalidate()方法或調用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. 僅有JSPaction元素:

<action path="/menu" 

    parameter="/default.jsp" 

    type="org.apache.struts.actions.ForwardAction" />

首先,ActionServlet接到請求後調用ForwardActionexecute()方法,execute()根據配置的parameter屬性值來forward到那個URI

這樣做的效果是:沒有任何form被實例化,比較現實的情形可能是formrequest更高級別的範圍中定義;或者這個action被用作在應用程序編譯好後充當系統參數,只需要更改這個配置文件而不需要重新編譯系統。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章