功能:
通过请求的参数来决定该执行哪一个方法,而不像一般的Action,从Execute方法执行。在一个窗体中包括两个以上同名的按钮时,由Struts来决定具体执行那个按钮操作,减少了Action类,增加了请求处理的灵活性
AppFuse1.7与AppFuse1.8的BaseAction区别
AppFuse1.7是继承LookupDispatchAction, 需要复写 getKeyMethodMap()方法
具体可参考http://blog.csdn.net/GOALSTAR/archive/2005/12/05/543802.aspx
AppFuse1.8是继承DispatchAction
AppFuse1.8中BaseAction的主要方法
1.getBean()方法,得到Bean的对象
public Object getBean (String name) {
if (ctx == null) {
ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(servlet.getServletContext());
}
return ctx.getBean(name);
}
2.convert() 对象转换方法,用来实现POJOs à ActionForm & ActionForm à POJOs 的转换
// 调用ConvertUtil#convert(java.lang.Object)
protected Object convert (Object o) throws Exception {
return ConvertUtil.convert(o);
}
3.execute()的方法
public ActionForward execute (ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
if (isCancelled(request)) {
try {
getMethod("cancel");
return dispatchMethod(mapping, form, request, response, "cancel");
} catch (NoSuchMethodException n) {
log.warn("No 'cancel' method found, returning null");
return cancelled(mapping, form, request, response);
}
}
// Check to see if methodName indicated by request parameter
String actionMethod = getActionMethodWithMapping(request, mapping);
if (actionMethod != null) {
return dispatchMethod(mapping, form, request, response, actionMethod);
} else {
String[] rules = {"edit", "save", "search", "view"};
for (int i = 0; i < rules.length; i++) {
// apply the rules for automatically appending the method name
if (request.getServletPath().indexOf(rules[i]) > -1) {
return dispatchMethod(mapping, form, request, response, rules[i]);
}
}
}
return super.execute(mapping, form, request, response);
}
在AppFuse1.8中BaseAction是继承DispatchAction , 使用struts时我们经常会用到DispatchAction.有了这个类,我们不需要针对每一个Action都要写一个特定的类,而是可以把一些相关的方法放到一个类中.
DispatchActon中使用了reflection来根据你传入的method参数的值来获取相应的参数来处理你的请求.正如他的方法 -- 他根据你传入的请求参数,用不同的方法来处理你的请求.
只要看看struts中DispatchAction(以下简写做DA)的源文件你就会发现,它有一个dispatchMethod方法,接受5个参数.其中4个就是我们通常的struts action里的(mapping,request,response,form),还有一个参数就是指定方法的参数的名字.
Ø Struts-config.xml中的配置
<action path="/editUser" ype="smarthings.webapp.action.UserAction" name="userForm" scope="request"
input="list" roles="admin" parameter="method" unknown="false" validate="false" >
<forward name="viewUsers" path="/editUser.html?method=Search( edit / save / delete )"/>
</action>
然后在Action中具体的实现Action都是继承了BaseAction的.比如
public final class PeopleAction extends BaseAction {
//具体的操作方法search
public ActionForward search(ActionMapping mapping, ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
//具体的操作方法cancel
public ActionForward cancel(ActionMapping mapping, ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {………}
//具体的操作方法delete
public ActionForward delete(ActionMapping mapping, ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {………}
//具体的操作方法Edit
public ActionForward edit(ActionMapping mapping, ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {……}
………
………
通过请求的参数来决定该执行哪一个方法,而不像一般的Action,从Execute方法执行。在一个窗体中包括两个以上同名的按钮时,由Struts来决定具体执行那个按钮操作,减少了Action类,增加了请求处理的灵活性
AppFuse1.7与AppFuse1.8的BaseAction区别
AppFuse1.7是继承LookupDispatchAction, 需要复写 getKeyMethodMap()方法
具体可参考http://blog.csdn.net/GOALSTAR/archive/2005/12/05/543802.aspx
AppFuse1.8是继承DispatchAction
AppFuse1.8中BaseAction的主要方法
1.getBean()方法,得到Bean的对象
public Object getBean (String name) {
if (ctx == null) {
ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(servlet.getServletContext());
}
return ctx.getBean(name);
}
2.convert() 对象转换方法,用来实现POJOs à ActionForm & ActionForm à POJOs 的转换
// 调用ConvertUtil#convert(java.lang.Object)
protected Object convert (Object o) throws Exception {
return ConvertUtil.convert(o);
}
3.execute()的方法
public ActionForward execute (ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
if (isCancelled(request)) {
try {
getMethod("cancel");
return dispatchMethod(mapping, form, request, response, "cancel");
} catch (NoSuchMethodException n) {
log.warn("No 'cancel' method found, returning null");
return cancelled(mapping, form, request, response);
}
}
// Check to see if methodName indicated by request parameter
String actionMethod = getActionMethodWithMapping(request, mapping);
if (actionMethod != null) {
return dispatchMethod(mapping, form, request, response, actionMethod);
} else {
String[] rules = {"edit", "save", "search", "view"};
for (int i = 0; i < rules.length; i++) {
// apply the rules for automatically appending the method name
if (request.getServletPath().indexOf(rules[i]) > -1) {
return dispatchMethod(mapping, form, request, response, rules[i]);
}
}
}
return super.execute(mapping, form, request, response);
}
在AppFuse1.8中BaseAction是继承DispatchAction , 使用struts时我们经常会用到DispatchAction.有了这个类,我们不需要针对每一个Action都要写一个特定的类,而是可以把一些相关的方法放到一个类中.
DispatchActon中使用了reflection来根据你传入的method参数的值来获取相应的参数来处理你的请求.正如他的方法 -- 他根据你传入的请求参数,用不同的方法来处理你的请求.
只要看看struts中DispatchAction(以下简写做DA)的源文件你就会发现,它有一个dispatchMethod方法,接受5个参数.其中4个就是我们通常的struts action里的(mapping,request,response,form),还有一个参数就是指定方法的参数的名字.
Ø Struts-config.xml中的配置
<action path="/editUser" ype="smarthings.webapp.action.UserAction" name="userForm" scope="request"
input="list" roles="admin" parameter="method" unknown="false" validate="false" >
<forward name="viewUsers" path="/editUser.html?method=Search( edit / save / delete )"/>
</action>
然后在Action中具体的实现Action都是继承了BaseAction的.比如
public final class PeopleAction extends BaseAction {
//具体的操作方法search
public ActionForward search(ActionMapping mapping, ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
//具体的操作方法cancel
public ActionForward cancel(ActionMapping mapping, ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {………}
//具体的操作方法delete
public ActionForward delete(ActionMapping mapping, ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {………}
//具体的操作方法Edit
public ActionForward edit(ActionMapping mapping, ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {……}
………
………