当前位置: 代码迷 >> XML/SOAP >> Struts-config.xml配置文件《action-地图pings》元素的详解
  详细解决方案

Struts-config.xml配置文件《action-地图pings》元素的详解

热度:1650   发布时间:2014-03-01 00:37:44.0
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还具有如下属性:
lscope:指定ActionForm Bean的作用域(session和request),缺省为session。(可选);
linput:当Bean发生错误时返回的路径,在validate验证框架中错误显示的页面(可选);
lclassname:指定一个调用这个Action类的ActionMapping类的全名。缺省用org.apache.struts.action.ActionMapping(可选);
linclude:如果没有forward的时候,它起forward的作用(可选);
lvalidate:若为true,则会调用ActionForm的validate()方法或调用validate验证,否则不调用,缺省为true(可选)。
parameter="method"这个参数就是说,在用户提交请求时取得method参数,根据method参数调用相应的方法,如 /editUser.html?method=Delete就是调用对应action中的Delete方法,这样你就可以写一个Action类处理很多的逻辑,而不是象从前那样在一个方法里面加上若干参数,或者直接建若干个action来处理。
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"<!--
当你没有传入method参数,或者没有符合参数的方法时,程序将执行unspecified方法;当然method只是一个逻辑名字而已,你也可以使用其他名字,如:method1,method2,go2,asdad等
--> type="org.apache.struts.actions.ForwardAction" />
这样做的效果是:没有任何form被实例化,比较现实的情形可能是form在request更高级别的范围中定义;或者这个action被用作在应用程序编译好后充当系统参数,只需要更改这个配置文件而不需要重新编译系统。
  相关解决方案