您的位置:首页 > 编程语言 > Java开发

如何写公共的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);
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: