如何写公共的strutsAction

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);
 }
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值