刚接触struts,到网上搜了不少相关内容,在学习dispatchaction时,到网上搜的帖子代码居然无法使用,后来问教员才知道自己出现的问题,于是拿出来和大家一起分享。
一般举例都是这样的:如下(<!-- -->之间是我搜的帖子内容)
<!--
DispatchAction类是一个抽象类,它实现了父类(Action)的execute()方法,所以它的子类就不用来实现这个方法了,只需要专注与实际操作的方法,
1.首先要一个DispatchAction的子类,它含有2个方法,pro1,pro2,
public class LoginAction extends DispatchAction {
public ActionForward pro1(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception {
System.out.println("pro1");
return mapping.findForward("success");
}
HttpServletRequest request, HttpServletResponse response) throws Exception {
System.out.println("pro1");
return mapping.findForward("success");
}
public ActionForward pro2(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception {
System.out.println("pro2");
return mapping.findForward("success");
}
HttpServletRequest request, HttpServletResponse response) throws Exception {
System.out.println("pro2");
return mapping.findForward("success");
}
}
一定要注意在DispatchAction 中所以你想执行的操作,都必须要有统一的参数(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response),是一个规定.
2.接下来是配置struts-config.xml
<action path="/Login1"
type="action.LoginAction"
name="TelForm"
attribute="tel"
scope="request"
parameter="action"
input="/jsp/dispatch1.jsp">
<forward name="success" path="/jsp/success1.jsp" />
</action>
type="action.LoginAction"
name="TelForm"
attribute="tel"
scope="request"
parameter="action"
input="/jsp/dispatch1.jsp">
<forward name="success" path="/jsp/success1.jsp" />
</action>
这里需要注意的就是parameter属性的值,因为这个值要和页面传来的参数对应.
3.再来看看JSP页jsp/dispatch1.jsp
添加如下的FORM
<form action="Login1.do?action=pro1">
username:<input name="username"/><br>
<input type="submit" value="submit"/>
</form>
这里要注意几点,首先 ?后面的KEY要和struts-config.xml中的parameter相同,还有它的VALUE要是你在action的一个方法名字
这里方法名为pro1, 那么在程序运行时就是调用pro1的操作,如果是pro2,那程序就调用pro2的操作.
-->
我照帖子所说做了恩编,但老报错,第二天去问教员才知道还有一个重要的地方掉了,就是form表单的
method属性必须为post,而且必须显示写出来
<form action="Login1.do?action=pro1" method="post">
希望能和大家一起分享,虽然很简单,但细节真的很重要!
小技巧
<a href="addUser.do?method=addUser&username=${classId}">Add User</a>
调用方法的同时传递值
DispatchAction本身就是扩展于Action,然后重写过Action中的execute方法,这是核心来的,在execute方法里头实现参数(parameter)功能。但是重新写过DispatchAction的核心方法execute,所以DispatchAction参数(parameter)功能就没能实现。
因为控制器直接调用Action子类的execute方法,所以就打印出"execute"
因为控制器直接调用Action子类的execute方法,所以就打印出"execute"
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);
}
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);
}
本文介绍了Struts框架中DispatchAction的使用方法,强调了form表单method属性的重要性,并通过实例详细展示了如何配置并实现多方法调用。
1万+

被折叠的 条评论
为什么被折叠?



