理解ActionMapping:
struts-config.xml文件中每个<action>标签都对应一个ActionMapping实例对象。<action-mappings>将特定请求映射到特定Action的相关信息存储在ActionMapping中,ActionServelt将ActionMapping传送到Action类的execute()方法,Action将使用ActionMapping的findForward()方法,此方法返回一个指定名称的ActionForward,这样Action就完成了本地转发。若没有找到具体的ActionForward,就返回一个null.
各属性的含义如下:
* path: 指定该Action接收的特定请求
* type: 指定该Action的类绝对路径
* forward: 只作页面跳转时可使用,用来减少Action数量.注意:这个不是<forward>标签
如:
<action path="/login1"
forward="/login.jsp">
</action>
上面的配置表明该Action接收页面的login1.do请求,然后不作任何逻辑处理直接跳转到login.jsp页面, 这样就避免了只为跳转而写Action
* name: 指定该Action关联的ActionForm
* scope: 默认为session,一般不是session范围要用到的数据不要设到session中,这样会无谓增加session负载,故尽量设成scope=request
* validate: 默认为false,设为true时会在页面收集数据前自动调用ActionForm中的validate方法
<action path="/login1"
type="cn.huan.struts.LoginAction"
name="loginForm"
scope="request"
validate="true"
input="/login1.jsp">
<forward name="success" path="/login_success.jsp"/>
</action>
<action path="/testunknow"
unknown="true"
forward="/testunknow.jsp">
</action>
上面的配置表明,如果客户端发送了没有定义的Action请求,则会交给该匿名ActionMapping来处理(上例是直接跳转到testunknow.jsp页面了)
ActionMapping配置综合实例:
<action-mappings>
<action path="/login"
type="cn.huan.struts.LoginAction"
name="loginForm"
scope="request"
>
<forward name="success" path="/login_success.jsp"/>
<!--
<forward name="error" path="/login_error.jsp"/>
-->
<forward name="error" path="/login.jsp"/>
</action>
<action path="/login1"
forward="/login.jsp">
</action>
<action path="/login2"
type="cn.huan.struts.LoginAction"
name="loginForm"
scope="request"
>
<forward name="success" path="/login_success.jsp"/>
<!--
<forward name="error" path="/login_error.jsp"/>
-->
<forward name="error" path="/login_by_strutstag.jsp"/>
</action>
<action path="/testunknow"
unknown="true"
forward="/testunknow.jsp">
</action>
<action path="/loginstrutstag"
forward="/login_by_strutstag.jsp">
</action>
</action-mappings>
........