FileDocCategorySizeDatePackage
ServletRedirectResult.javaAPI DocExample6218Mon Jul 23 13:26:38 BST 2007org.apache.struts2.dispatcher

ServletRedirectResult

public class ServletRedirectResult extends StrutsResultSupport
Calls the {@link HttpServletResponse#sendRedirect(String) sendRedirect} method to the location specified. The response is told to redirect the browser to the specified location (a new request from the client). The consequence of doing this means that the action (action instance, action errors, field errors, etc) that was just executed is lost and no longer available. This is because actions are built on a single-thread model. The only way to pass data is through the session or with web parameters (url?name=value) which can be OGNL expressions.

This result type takes the following parameters:

  • location (default) - the location to go to after execution.
  • parse - true by default. If set to false, the location param will not be parsed for Ognl expressions.

This result follows the same rules from {@link StrutsResultSupport}.

Example:

<result name="success" type="redirect">
<param name="location">foo.jsp</param>
<param name="parse">false</param>
</result>

Fields Summary
private static final long
serialVersionUID
private static final Log
log
protected boolean
prependServletContext
protected org.apache.struts2.dispatcher.mapper.ActionMapper
actionMapper
Constructors Summary
public ServletRedirectResult()


      
        super();
    
public ServletRedirectResult(String location)

        super(location);
    
Methods Summary
protected voiddoExecute(java.lang.String finalLocation, com.opensymphony.xwork2.ActionInvocation invocation)
Redirects to the location specified by calling {@link HttpServletResponse#sendRedirect(String)}.

param
finalLocation the location to redirect to.
param
invocation an encapsulation of the action execution state.
throws
Exception if an error occurs when redirecting.

        ActionContext ctx = invocation.getInvocationContext();
        HttpServletRequest request = (HttpServletRequest) ctx.get(ServletActionContext.HTTP_REQUEST);
        HttpServletResponse response = (HttpServletResponse) ctx.get(ServletActionContext.HTTP_RESPONSE);

        if (isPathUrl(finalLocation)) {
            if (!finalLocation.startsWith("/")) {
                ActionMapping mapping = actionMapper.getMapping(
                        request, Dispatcher.getInstance().getConfigurationManager()); 
                String namespace = null;
                if (mapping != null) {
                    namespace = mapping.getNamespace();
                }

                if ((namespace != null) && (namespace.length() > 0) && (!"/".equals(namespace))) {
                    finalLocation = namespace + "/" + finalLocation;
                } else {
                    finalLocation = "/" + finalLocation;
                }
            }

            // if the URL's are relative to the servlet context, append the servlet context path
            if (prependServletContext && (request.getContextPath() != null) && (request.getContextPath().length() > 0)) {
                finalLocation = request.getContextPath() + finalLocation;
            }

            finalLocation = response.encodeRedirectURL(finalLocation);
        }

        if (log.isDebugEnabled()) {
            log.debug("Redirecting to finalLocation " + finalLocation);
        }

        response.sendRedirect(finalLocation);
    
private static booleanisPathUrl(java.lang.String url)

        // filter out "http:", "https:", "mailto:", "file:", "ftp:"
        // since the only valid places for : in URL's is before the path specification
        // either before the port, or after the protocol
        return (url.indexOf(':") == -1);
    
public voidsetActionMapper(org.apache.struts2.dispatcher.mapper.ActionMapper mapper)

        this.actionMapper = mapper;
    
public voidsetPrependServletContext(boolean prependServletContext)
Sets whether or not to prepend the servlet context path to the redirected URL.

param
prependServletContext true to prepend the location with the servlet context path, false otherwise.

        this.prependServletContext = prependServletContext;