KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > webwork > dispatcher > ServletDispatcherResult


1 /*
2  * Copyright (c) 2002-2003 by OpenSymphony
3  * All rights reserved.
4  */

5 package com.opensymphony.webwork.dispatcher;
6
7 import com.opensymphony.webwork.ServletActionContext;
8 import com.opensymphony.xwork.ActionInvocation;
9 import org.apache.commons.logging.Log;
10 import org.apache.commons.logging.LogFactory;
11
12 import javax.servlet.RequestDispatcher JavaDoc;
13 import javax.servlet.http.HttpServletRequest JavaDoc;
14 import javax.servlet.http.HttpServletResponse JavaDoc;
15 import javax.servlet.jsp.PageContext JavaDoc;
16
17
18 /**
19  * Includes or forwards a view. There are three possible ways the result can be executed: <ul>
20  * <p/>
21  * <li>If we are in the scope of a JSP (a PageContext is available), PageContext's
22  * {@link PageContext#include(String) include} method is called.</li>
23  * <p/>
24  * <li>If there is no PageContext and we're not in any sort of include (there is no
25  * "javax.servlet.include.servlet_path" in the request attributes), then a call to
26  * {@link RequestDispatcher#forward(javax.servlet.ServletRequest, javax.servlet.ServletResponse) forward}
27  * is made.</li>
28  * <p/>
29  * <li>Otherwise, {@link RequestDispatcher#include(javax.servlet.ServletRequest, javax.servlet.ServletResponse) include}
30  * is called.</li></ul>
31  * <p/>
32  * This result follows the same rules from {@link WebWorkResultSupport}.
33  *
34  * @author Patrick Lightbody
35  * @see javax.servlet.RequestDispatcher
36  */

37 public class ServletDispatcherResult extends WebWorkResultSupport {
38     //~ Static fields/initializers /////////////////////////////////////////////
39

40     private static final Log log = LogFactory.getLog(ServletDispatcherResult.class);
41
42     //~ Methods ////////////////////////////////////////////////////////////////
43

44     /**
45      * Dispatches to the given location. Does its forward via a RequestDispatcher. If the
46      * dispatch fails a 404 error will be sent back in the http response.
47      *
48      * @param finalLocation the location to dispatch to.
49      * @param invocation the execution state of the action
50      * @throws Exception if an error occurs. If the dispatch fails the error will go back via the
51      * HTTP request.
52      */

53     public void doExecute(String JavaDoc finalLocation, ActionInvocation invocation) throws Exception JavaDoc {
54         if (log.isDebugEnabled()) {
55             log.debug("Forwarding to location " + finalLocation);
56         }
57
58         PageContext JavaDoc pageContext = ServletActionContext.getPageContext();
59
60         if (pageContext != null) {
61             pageContext.include(finalLocation);
62         } else {
63             HttpServletRequest JavaDoc request = ServletActionContext.getRequest();
64             HttpServletResponse JavaDoc response = ServletActionContext.getResponse();
65             RequestDispatcher JavaDoc dispatcher = request.getRequestDispatcher(finalLocation);
66
67             // if the view doesn't exist, let's do a 404
68
if (dispatcher == null) {
69                 response.sendError(404, "result '" + finalLocation + "' not found");
70
71                 return;
72             }
73
74             // If we're included, then include the view
75
// Otherwise do forward
76
// This allow the page to, for example, set content type
77
if (!response.isCommitted() && (request.getAttribute("javax.servlet.include.servlet_path") == null)) {
78                 request.setAttribute("webwork.view_uri", finalLocation);
79                 request.setAttribute("webwork.request_uri", request.getRequestURI());
80
81                 dispatcher.forward(request, response);
82             } else {
83                 dispatcher.include(request, response);
84             }
85         }
86     }
87 }
88
Popular Tags