Struts新用法以及关于理解

来源:互联网 发布:jquery ajax json实例 编辑:程序博客网 时间:2024/05/21 10:15

 

(一)

Struts中的action动作自动生成的都会继承action类,也就是同时也要复写execuse()方法从这里入口执行动作。

另一种用法也可不用继承action这样执行。提供一个类MappingDispatchAction,当继承这个类就可以不用复写execuse()方法从这里唯一入口了。

入口的方式是在配置文件struts-config.xml中,在action中加入<set-property property="parameter" value="方法名 "/>就可以进入想进的方法体中了。

 

例如:

struts-config.xml文件中action

<action path="/login" scope="request"

name="loginForm"

           input="/login.jsp" type="action.UserAction"validate="true">

    <set-property property="parameter" value="login" />

</action>

在类action.UserAction

public class UserAction extends MappingDispatchAction {

 

public ActionForward login(ActionMapping mapping, ActionForm form,

           HttpServletRequest request, HttpServletResponse response)

           throws Exception {

           //TODO

}

}

这样就可以完成action了,提交时会找到login方法执行。

(二)重定向

 

<action path="/accountInfo" type="action.UserAction" name="modifyForm" validate="false" scope="request">

       <set-property property="parameter" value="accountInfo"/>

       <forward name="success" path="/accountInfo.jsp" redirect="false"/>

</action>

<action path="/login" scope="request" name="loginForm"

           input="/login.jsp" type="action.UserAction"

           validate="true">

           <set-property property="parameter" value="login" />

           <forward name="userHome" path="/accountInfo.do" redirect="true/false"/>

</action>

action类代码中执行

return mapping.findForward(“userHome”);

当提交login.do会把数据提交给loginForm服务请求,跳转到userHome会根据path找到路径,而这里的path是另一个action,他也就会调用另一个action执行,这时把redirect设置成true重定向,执行下一个action客户端会再次发出请求这时候使用的就是modifyForm而不是loginForm,之前的请求不起作用。redirect设置成false取消重定向,依然会执行下一个action但客户端不会再发出请求,保留loginForm中信息,两个action使用的是相同的form

例如

页面A提交请求login.do,执行action.UserAction跳转userHome

页面B

<bean:write name="loginForm" property="name"/>

 

依照上面的配置文件,如果redirect="true" 就会报错 找不到loginForm

如果redirect="false" OK

问题就在重定向后传递使用的是modifyForm,取消重定向使用的依然是loginForm