DispatchAction使用时碰到的问题

this_is_alan 2005-05-10 10:38:59
jsp页面的代码:

<html:form action="/testCase">
<html:hidden property="action" value="update" />
<html:submit/><html:cancel/>
</html:form>

struts-config 中的配置:
<action
attribute="testCaseForm"
input="/jsps/testCase.jsp"
name="testCaseForm"
path="/testCase"
scope="request"
parameter="action"
type="com.sf.struts.action.TestCaseAction">
</action>

action中的代码
public class TestCaseAction extends DispatchAction {

public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
TestCaseForm testCaseForm = (TestCaseForm) form;
System.out.println("execute");

return mapping.findForward("USERLOGIN");
}

public ActionForward update(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
TestCaseForm testCaseForm = (TestCaseForm) form;
System.out.println("update");
return mapping.findForward("USERLOGIN");

}
}

期望出现 "update",但最终总是打印出execute,实在想不通怎么回事,请有经验的高手指教!
...全文
91 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
this_is_alan 2005-05-11
  • 打赏
  • 举报
回复
:)谢啦
TyroneChan 2005-05-10
  • 打赏
  • 举报
回复
当然是打印出"execute"啦,而且接照你这种写法,你这个TestCaseAction就完全是一个Action,而不是一个DispatchAction。

原因如下:

因为DispatchAction本身就是扩展于Action,然后重写过Action中的execute方法,这是核心来的,在execute方法里头实现参数(parameter)功能。但是你上面的代码中,你又重新写过DispatchAction的核心方法execute,所以DispatchAction参数(parameter)功能就没能实现。
因为控制器直接调用Action子类的execute方法,所以就打印出"execute"。

解决方法:
不要在DispatchAction的子类重写execute方法,即删除掉TestCaseAction中的execute方法

建议去看看struts的源代码

以下是DispatchAction.java的部分源代码:
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {

// Identify the request parameter containing the method name
String parameter = mapping.getParameter();
if (parameter == null) {
String message =
messages.getMessage("dispatch.handler", mapping.getPath());
log.error(message);
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
message);
return (null);
}

// Identify the method name to be dispatched to.
// dispatchMethod() will call unspecified() if name is null
String name = request.getParameter(parameter);

// Invoke the named method, and return the result
return dispatchMethod(mapping, form, request, response, name);
}

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧