`
gutou9
  • 浏览: 140736 次
  • 性别: Icon_minigender_1
  • 来自: 大连
社区版块
存档分类
最新评论

Xwork2 源码阅读(五)

阅读更多

在前边的几篇中,我们已经看了Action的执行,

实际上 Action 的任务已经基本完成了,只剩下一个收尾的工作 : Action的跳转。

 

让我们看一下 Xwork 是如何做的。

相关的方法是我们前边提到过的 ActionInvocation 的 invoke 方法,

 

// now execute the result, if we're supposed to
if (proxy.getExecuteResult()) {
    executeResult();
} 



   /**
     * Uses getResult to get the final Result and executes it
     * 
     * @throws ConfigurationException If not result can be found with the returned code
     */
    private void executeResult() throws Exception {
        result = createResult();

        String timerKey = "executeResult: "+getResultCode();
        try {
            UtilTimerStack.push(timerKey);
            if (result != null) {
                result.execute(this);
            } else if (resultCode != null && !Action.NONE.equals(resultCode)) {
                throw new ConfigurationException("No result defined for action " + getAction().getClass().getName() 
                        + " and result " + getResultCode(), proxy.getConfig());
            } else {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("No result returned for action "+getAction().getClass().getName()+" at "+proxy.getConfig().getLocation());
                }
            }
        } finally {
            UtilTimerStack.pop(timerKey);
        }
    }

 

result.execute(this);
调用result 的 相应方法,

 

ServletDispatcherResult 是 xwork 中,负责转跳页面的Result,

我们以他做例子,看一下 xwork 的Result 是怎么做的。

 

public class ServletDispatcherResult extends WebWorkResultSupport {

    private static final Log log = LogFactory.getLog(ServletDispatcherResult.class);


    /**
     * Dispatches to the given location. Does its forward via a RequestDispatcher. If the
     * dispatch fails a 404 error will be sent back in the http response.
     *
     * @param finalLocation the location to dispatch to.
     * @param invocation    the execution state of the action
     * @throws Exception if an error occurs. If the dispatch fails the error will go back via the
     *                   HTTP request.
     */
    public void doExecute(String finalLocation, ActionInvocation invocation) throws Exception {
        if (log.isDebugEnabled()) {
            log.debug("Forwarding to location " + finalLocation);
        }

        PageContext pageContext = ServletActionContext.getPageContext();

        if (pageContext != null) {
            pageContext.include(finalLocation);
        } else {
            HttpServletRequest request = ServletActionContext.getRequest();
            HttpServletResponse response = ServletActionContext.getResponse();
            RequestDispatcher dispatcher = request.getRequestDispatcher(finalLocation);

            // if the view doesn't exist, let's do a 404
            if (dispatcher == null) {
                response.sendError(404, "result '" + finalLocation + "' not found");

                return;
            }

            // If we're included, then include the view
            // Otherwise do forward 
            // This allow the page to, for example, set content type 
            if (!response.isCommitted() && (request.getAttribute("javax.servlet.include.servlet_path") == null)) {
                request.setAttribute("webwork.view_uri", finalLocation);
                request.setAttribute("webwork.request_uri", request.getRequestURI());

                dispatcher.forward(request, response);
            } else {
                dispatcher.include(request, response);
            }
        }
    }
}

 

doExecute 方法,是Result 的主要逻辑,

我们可以看到

 RequestDispatcher dispatcher = request.getRequestDispatcher(finalLocation);

 dispatcher.forward(request, response);
 这两句实现了页面的转跳功能,将执行完毕的Acion 跳到适当的页面。

 

这是我们会最常用到的一个 Result,

其他还有很多,可以生成word,pdf,或转跳到另一个Action 等,都于此类似,

 

Xwork中, Action的调用,到此也就基本完成了。

 

 

 

 

 

 

 

 

 

 

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics