我按照网上说的 首先是类继承DispatchAction ,然后写了些测试代码 代码如下:
public class LoginAction extends DispatchAction { //继承了DispatchAction
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
request.getSession().setAttribute("operation", "execute");
return mapping.findForward("error.jsp");
}
//login方法
public ActionForward login(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
request.getSession().setAttribute("operation", "login");
return mapping.findForward("error.jsp");
}
//register方法
public ActionForward register(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
request.getSession().setAttribute("operation", "register");
return mapping.findForward("error.jsp");
}
}
然后struts配置文件中parameter="operation"
<action
path="/login"
type="com.mulan.struts.action.LoginAction"
scope="request"
input="/index.jsp"
validate="false"
parameter="operation" >
</action>
然后index.jsp文件
<form action="login.do?operation='login'" method="post">
用户名:<input type="text" name="temail"><br>
密码:<input type="password" name="tpass"><br>
<input type="submit" value="登录">
<a href="register.jsp">加入我们</a>
</form>
照理来说 提交以后 应该调用login方法,可是好像没反应.
地址栏上的地址变成了 [path]/login.do?operation='login'
------解决方案--------------------
DispatchAction继承自Action类,它是一个抽象类,封装了一些基础方法,来解决使用一个Action处理多个操作的能力,这就是DispatchAction最大的用途,它可以帮助我们用一个Action类,封装一套类似的操作方法,节省了类的数目,同时也减轻了后期维护的困难。
struts1.x里面的
------解决方案--------------------
<form action="login.do?operation=login" method="post">
去掉引号
------解决方案--------------------
不是用!号吗?难道用?号
------解决方案--------------------
<form action="login!login" method="post">应该是这样。
------解决方案--------------------
execute 方法中 接受 operation的值, 然后判断是login了 传入 login判断中
------解决方案--------------------
这是dispatchaction的execute方法。 我刚看了下。 dispatchaction应该就是通过她本书execute方法来实现一个类实现多个功能。
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
if (isCancelled(request)) {
ActionForward af = cancelled(mapping, form, request, response);