Struts 1.2.2 新功能体验(二)

类别:Java 点击:0 评论:0 推荐:

从1.2.2开始对DispatchAction中实现isCancelled的功能进行了简化.
在DispatchAction的子类中只需override DispatchAction的cancelled
方法即可,而不用在1.1中要通过override execute方法来实现.
  至于实现的原理可参见下面的Struts1.1.2中DispatchAction源代码.


    /**
     * Process the specified HTTP request, and create the corresponding HTTP
     * response (or forward to another web component that will create it).
     * Return an <code>ActionForward</code> instance describing where and how
     * control should be forwarded, or <code>null</code> if the response has
     * already been completed.
     *
     * @param mapping The ActionMapping used to select this instance
     * @param form The optional ActionForm bean for this request (if any)
     * @param request The HTTP request we are processing
     * @param response The HTTP response we are creating
     *
     * @exception Exception if an exception occurs
     */
    public ActionForward execute(ActionMapping mapping,
                                 ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response)
            throws Exception {
 //请注意下面1.2.2新增的代码
        if (isCancelled(request)) {
            ActionForward af = cancelled(mapping, form, request, response);
            if (af != null) {
                return af;
            }
        }
        // Identify the request parameter containing the method name
        String parameter = mapping.getParameter();
        if (parameter == null) {
            String message =
                    messages.getMessage("dispatch.handler", mapping.getPath());

            log.error(message);

            throw new ServletException(message);
        }

        // Get the method's name. This could be overridden in subclasses.
        String name = getMethodName(mapping, form, request, response, parameter);


 // Prevent recursive calls
 if ("execute".equals(name) || "perform".equals(name)){
  String message =
   messages.getMessage("dispatch.recursive", mapping.getPath());

  log.error(message);
  throw new ServletException(message);
 }

 

    /**
     * 在DispatchAction子类中需要Override此方法!
     * Method which is dispatched to when the request is a cancel button submit.
     * Subclasses of <code>DispatchAction</code> should override this method if
     * they wish to provide default behavior different than returning null.
     * @since Struts 1.2.0
     */
    protected ActionForward cancelled(ActionMapping mapping,
                                      ActionForm form,
                                      HttpServletRequest request,
                                      HttpServletResponse response)
            throws Exception {

        return null;
    }

本文地址:http://com.8s8s.com/it/it15688.htm