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子元素。除了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>

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被用作在应用程序编译好后充当系统参数,只需要更改这个配置文件而不需要重新编译系统。

Struts-config.xml配置文件《action-mappings》元素的详解,,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。