我按照网上说的 首先是类继承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'
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'
问题是应该怎么使用它呢 我的代码有没有什么问题 ?
你把你那session里面的值那名字换下试试,我经常出现重名导致一些问题~ 总之多试下
HttpServletRequest request, HttpServletResponse response)
throws Exception {
if (isCancelled(request)) {
ActionForward af = cancelled(mapping, form, request, response); if (af != null) {
return af;
}
} // Get the parameter. This could be overridden in subclasses.
String parameter = getParameter(mapping, form, request, response); // Get the method's name. This could be overridden in subclasses.
String name =
getMethodName(mapping, form, request, response, parameter); // Prevent recursive calls
if ("execute".equals(name) || "perform".equals(name)) {
String message =
messages.getMessage("dispatch.recursive", mapping.getPath()); log.error(message);
throw new ServletException(message);
} // Invoke the named method, and return the result
return dispatchMethod(mapping, form, request, response, name);
}
还有6楼写的那个是struts2里面的。 那个!可以直接调用action里的方法
恩恩 谢谢 按照你的思路 把问题解决了 ..
其实 .. 问题有两个
一个是因为我页面跳转出错了
另外一个 不知道是不是因为我重写了 execute 方法 struts总是跑到 execute里面去 总之谢谢大家了 结贴了