
com.opensymphony.webwork.dispatcher.ServletDispatcherResult Maven / Gradle / Ivy
/*
* Copyright (c) 2002-2003 by OpenSymphony
* All rights reserved.
*/
package com.opensymphony.webwork.dispatcher;
import com.opensymphony.webwork.ServletActionContext;
import com.opensymphony.xwork.ActionInvocation;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.PageContext;
/**
*
*
* Includes or forwards to a view (usually a jsp). Behind the scenes WebWork
* will use a RequestDispatcher, where the target servlet/JSP receives the same
* request/response objects as the original servlet/JSP. Therefore, you can pass
* data between them using request.setAttribute() - the WebWork action is
* available.
*
* There are three possible ways the result can be executed:
*
*
*
* - If we are in the scope of a JSP (a PageContext is available), PageContext's
* {@link PageContext#include(String) include} method is called.
*
* - If there is no PageContext and we're not in any sort of include (there is no
* "javax.servlet.include.servlet_path" in the request attributes), then a call to
* {@link RequestDispatcher#forward(javax.servlet.ServletRequest, javax.servlet.ServletResponse) forward}
* is made.
*
* - Otherwise, {@link RequestDispatcher#include(javax.servlet.ServletRequest, javax.servlet.ServletResponse) include}
* is called.
*
*
*
*
* This result type takes the following parameters:
*
*
*
*
*
* - location (default) - the location to go to after execution (ex. jsp).
*
* - parse - true by default. If set to false, the location param will not be parsed for Ognl expressions.
*
*
*
*
*
* Example:
*
*
* <result name="success" type="dispatcher">
* <param name="location">foo.jsp</param>
* </result>
*
*
* This result follows the same rules from {@link WebWorkResultSupport}.
*
* @author Patrick Lightbody
* @see javax.servlet.RequestDispatcher
*/
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);
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy