package cn.taxgis.product.app.sms.common;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
public class GenericAction extends DispatchAction {
/**
* 覆盖DispatchAction, 这个是getMethod()方法用于查找方法的参数类型表.
* 考虑到我们的开发不需要使用ActionMapping, 将其从参数表中删除.
*/
protected Class types3[] =
{
ActionForm.class,
HttpServletRequest.class,
HttpServletResponse.class };
protected Class types2[] = {HttpServletRequest.class,
HttpServletResponse.class };
/**
* copy from
* Dispatch to the specified method.
* @since Struts 1.1
*
*
*/
protected ActionForward dispatchMethod(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response,
String name)
throws Exception {
// Make sure we have a valid method name to call.
// This may be null if the user hacks the query string.
if (name == null) {
return this.unspecified(mapping, form, request, response);
}
// Identify the method object to be dispatched to
MethodDefinition method = null;
try {
method = getMethodDefinition(name);
} catch (NoSuchMethodException e) {
String message =
messages.getMessage("dispatch.method", mapping.getPath(), name);
log.error(message, e);
response.sendError(
HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
message);
return (null);
}
ActionForward forward = null;
try {
// 修改了try块中的代码.
// 不需要mapping 参数.
//Object args[] = { mapping, form, request, response };
//forward = (ActionForward) method.invoke(this, args);
/*Object[] args = { form, request, response };
String forwardname = (String) method.invoke(this, args);*/
String forwardname = null;
if(method.argNumber == 2) {
forwardname = (String) method.method.invoke(this, new Object[]{request, response});
} else {
forwardname = (String) method.method.invoke(this, new Object[]{form, request, response});
}
//System.out.println("forwardname:[" + forwardname + "]");
if (forwardname == null){
forward = null;
}else{
forward = mapping.findForward(forwardname);
}
if (log.isDebugEnabled()){
log.debug("Action执行完 "+name+" 方法,forward向"+forwardname);
}
//System.out.println("Action执行完 "+name+" 方法,forward向"+forwardname);
} catch (ClassCastException e) {
String message =
messages.getMessage("dispatch.return", mapping.getPath(), name);
messages.getMessage(
"dispatch.error",
mapping.getPath(),
name);
log.error(message, e);
response.sendError(
HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
message);
return (null);
}
// Return the returned ActionForward instance
return (forward);
}
/**
* Introspect the current class to identify a method of the specified
* name that accepts the same parameter types as the <code>execute</code>
* method does.
*
* @param name Name of the method to be introspected
*
* @exception NoSuchMethodException if no such method can be found
*/
protected MethodDefinition getMethodDefinition(String name)
throws NoSuchMethodException {
synchronized (methods) {
MethodDefinition method = (MethodDefinition) methods.get(name);
if (method == null) {
method = new MethodDefinition();
Method m = null;
try {
m = clazz.getMethod(name, types2);
method.method = m;
method.argNumber = 2;
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
m = clazz.getMethod(name, types3);
method.method = m;
method.argNumber = 3;
}
methods.put(name, method);
}
return (method);
}
}
private static class MethodDefinition {
Method method;
int argNumber;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
GenericAction.rar (1个子文件)
GenericAction.java 4KB
共 1 条
- 1
资源评论
wfx1018
- 粉丝: 6
- 资源: 28
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功