如何写公共的strutsAction
2007-12-17 16:02
387 查看
1.dispatchMethod方法是org.apache.struts.actions.DispatchAction中的protected方法.
protected ActionForward dispatchMethod(ActionMapping mapping,
ActionForm form,
javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response,
java.lang.String name)
throws java.lang.Exception
Dispatch to the specified method. 分配(调用)给一个指定的方法.
protected java.lang.reflect.Method getMethod(java.lang.String name)
throws java.lang.NoSuchMethodException
Introspect the current class to identify a method of the specified name that accepts
the same parameter types as the execute method does.
protected ActionForward unspecified(ActionMapping mapping,
ActionForm form,
javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response)
throws java.lang.ExceptionMethod
which is dispatched to when there is no value for specified request parameter included in the request. Subclasses of DispatchAction should override this method if they wish to provide default behavior different than producing an HTTP "Bad Request" error.
在DispatchAction中子类中,执行Action中的任务方法前,
其父类中dispatchMethod() will call first.然后才去执行那个Action.
如果 unspecified() will call ,if name is null.
所以说,在Action处理时,针对form的操作可以重写在dispatchMethod()方法内.如验证是否重复提交.
2.
//saveToken(javax.servlet.http.HttpServletRequest request)
// Save a new transaction token in the user's current session, creating a new session if necessary.
如新建时调用这个方法.
//resetToken(javax.servlet.http.HttpServletRequest request)
// Reset the saved transaction token in the user's session.
isTokenValid(request) //检查是否重复提交.
public class CommonAction extends DispatchAction{
protected ActionForward dispatchMethod(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response,
String parameter)
throws Exception {
if (form instanceof CommonForm ) {
CommonForm commonForm = (CommonForm) form;
if (commonForm.isInit()) {
saveToken(request);
//saveToken(javax.servlet.http.HttpServletRequest request)
//Save a new transaction token in the user's current session, creating a new session if necessary.
} else if (!isTokenValid(request)) {
resetToken(request);
//resetToken(javax.servlet.http.HttpServletRequest request)
// Reset the saved transaction token in the user's session.
_logger.info("token error ");
return mapping.findForward("fail");
} else{
ActionForward returnForward = super.dispatchMethod(mapping,form,request,response,parameter);
resetToken(request);
return returnForward;
}
}
return super.dispatchMethod(mapping,form,request,response,parameter);
}
protected void setErrorMsg(HttpServletRequest request, Exception e) {
e.printStackTrace();
ActionMessages errors = new ActionMessages();
errors.add(ActionErrors.GLOBAL_MESSAGE, new ActionMessage(
"errors.exception", e.getMessage(), e));
saveErrors(request, errors);
}
}
protected ActionForward dispatchMethod(ActionMapping mapping,
ActionForm form,
javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response,
java.lang.String name)
throws java.lang.Exception
Dispatch to the specified method. 分配(调用)给一个指定的方法.
protected java.lang.reflect.Method getMethod(java.lang.String name)
throws java.lang.NoSuchMethodException
Introspect the current class to identify a method of the specified name that accepts
the same parameter types as the execute method does.
protected ActionForward unspecified(ActionMapping mapping,
ActionForm form,
javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response)
throws java.lang.ExceptionMethod
which is dispatched to when there is no value for specified request parameter included in the request. Subclasses of DispatchAction should override this method if they wish to provide default behavior different than producing an HTTP "Bad Request" error.
在DispatchAction中子类中,执行Action中的任务方法前,
其父类中dispatchMethod() will call first.然后才去执行那个Action.
如果 unspecified() will call ,if name is null.
所以说,在Action处理时,针对form的操作可以重写在dispatchMethod()方法内.如验证是否重复提交.
2.
//saveToken(javax.servlet.http.HttpServletRequest request)
// Save a new transaction token in the user's current session, creating a new session if necessary.
如新建时调用这个方法.
//resetToken(javax.servlet.http.HttpServletRequest request)
// Reset the saved transaction token in the user's session.
isTokenValid(request) //检查是否重复提交.
public class CommonAction extends DispatchAction{
protected ActionForward dispatchMethod(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response,
String parameter)
throws Exception {
if (form instanceof CommonForm ) {
CommonForm commonForm = (CommonForm) form;
if (commonForm.isInit()) {
saveToken(request);
//saveToken(javax.servlet.http.HttpServletRequest request)
//Save a new transaction token in the user's current session, creating a new session if necessary.
} else if (!isTokenValid(request)) {
resetToken(request);
//resetToken(javax.servlet.http.HttpServletRequest request)
// Reset the saved transaction token in the user's session.
_logger.info("token error ");
return mapping.findForward("fail");
} else{
ActionForward returnForward = super.dispatchMethod(mapping,form,request,response,parameter);
resetToken(request);
return returnForward;
}
}
return super.dispatchMethod(mapping,form,request,response,parameter);
}
protected void setErrorMsg(HttpServletRequest request, Exception e) {
e.printStackTrace();
ActionMessages errors = new ActionMessages();
errors.add(ActionErrors.GLOBAL_MESSAGE, new ActionMessage(
"errors.exception", e.getMessage(), e));
saveErrors(request, errors);
}
}
相关文章推荐
- Struts 1.2 中如何测试Action
- 如何在struts项目中让标签的page属性的值可以是action的路径
- [JavaEE]Struts2路径问题(如何在jsp页面正确访问struts.xml中的action)
- Jsp中如何接收Struts的Action中放入值栈中的内容
- 如何理解Struts和Spring单例和多例,以及Action请求的问题
- Struts 2中如何用一个action调用多个方法,避免每一个方法写一个action的繁琐之处
- S4.1_Struts2_ActionAdvance 在action动作类里定义增删改方法,在struts.xml中的action method属性如何设置?
- 在struts2.0的action中如何弹出信息提示
- Struts 1 中如何测试Action
- Struts 2 的Action中如何获取request、response、session、application等web元素(1)
- 如何使用Spring来管理Struts中的Action
- Struts 1.2 如何测试Action
- struts的ui标签,theme(主题)(如何去掉fielderror actionerror actionmessage前的小点,让其按自己要求显示的5种方法)
- struts中如何将前台的值能在action中获取到
- struts中 如何找action对应的jsp
- Struts 2 的Action中如何获取request、response、session、application等web元素(2)
- struts中的action和Action类以及struts2中如何在Action类中获得servlet api的域对象
- 如何成功调试《Struts in Action》的第一例
- 如何从action传递参数到struts配置文件
- Struts的控制器如何知道什么样的信息转发到什么样的Action类呢?