刚接触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);
}