FileDocCategorySizeDatePackage
ServletActionRedirectResult.javaAPI DocExample8403Mon Jul 23 13:26:38 BST 2007org.apache.struts2.dispatcher

ServletActionRedirectResult

public class ServletActionRedirectResult extends ServletRedirectResult
This result uses the {@link ActionMapper} provided by the {@link ActionMapperFactory} to redirect the browser to a URL that invokes the specified action and (optional) namespace. This is better than the {@link ServletRedirectResult} because it does not require you to encode the URL patterns processed by the {@link ActionMapper} in to your struts.xml configuration files. This means you can change your URL patterns at any point and your application will still work. It is strongly recommended that if you are redirecting to another action, you use this result rather than the standard redirect result.

See examples below for an example of how request parameters could be passed in. This result type takes the following parameters:

  • actionName (default) - the name of the action that will be redirect to
  • namespace - used to determine which namespace the action is in that we're redirecting to . If namespace is null, this defaults to the current namespace
Example:

<package name="public" extends="struts-default">
<action name="login" class="...">
<!-- Redirect to another namespace -->
<result type="redirect-action">
<param name="actionName">dashboard</param>
<param name="namespace">/secure</param>
</result>
</action>
</package>

<package name="secure" extends="struts-default" namespace="/secure">
<-- Redirect to an action in the same namespace -->
<action name="dashboard" class="...">
<result>dashboard.jsp</result>
<result name="error" type="redirect-action>error</result>
</action>

<action name="error" class="...">
<result>error.jsp</result>
</action>
</package>

<package name="passingRequestParameters" extends="struts-default" namespace="/passingRequestParameters">
<-- Pass parameters (reportType, width and height) -->
<!--
The redirect-action url generated will be :
/genReport/generateReport.action?reportType=pie&width=100&height=100
-->
<action name="gatherReportInfo" class="...">
<result name="showReportResult" type="redirect-action">
<param name="actionName">generateReport</param>
<param name="namespace">/genReport</param>
<param name="reportType">pie</param>
<param name="width">100</param>
<param name="height">100</param>
</result>
</action>
</package>


see
ActionMapper

Fields Summary
private static final long
serialVersionUID
public static final String
DEFAULT_PARAM
The default parameter
protected String
actionName
protected String
namespace
protected String
method
private Map
requestParameters
protected List
prohibitedResultParam
Constructors Summary
public ServletActionRedirectResult()


      
        super();
    
public ServletActionRedirectResult(String actionName)

        this(null, actionName, null);
    
public ServletActionRedirectResult(String actionName, String method)

        this(null, actionName, method);
    
public ServletActionRedirectResult(String namespace, String actionName, String method)

        super(null);
        this.namespace = namespace;
        this.actionName = actionName;
        this.method = method;
    
Methods Summary
public org.apache.struts2.dispatcher.ServletActionRedirectResultaddParameter(java.lang.String key, java.lang.Object value)
Adds a request parameter to be added to the redirect url

param
key The parameter name
param
value The parameter value

        requestParameters.put(key, String.valueOf(value));
        return this;
    
public voidexecute(com.opensymphony.xwork2.ActionInvocation invocation)

see
com.opensymphony.xwork2.Result#execute(com.opensymphony.xwork2.ActionInvocation)


           
          
        actionName = conditionalParse(actionName, invocation);
        if (namespace == null) {
            namespace = invocation.getProxy().getNamespace();
        } else {
            namespace = conditionalParse(namespace, invocation);
        }
        if (method == null) {
            method = "";
        }
        else {
            method = conditionalParse(method, invocation);
        }

        String resultCode = invocation.getResultCode();
        if (resultCode != null) {
            ResultConfig resultConfig = invocation.getProxy().getConfig().getResults().get(
                    resultCode);
            Map resultConfigParams = resultConfig.getParams();
            for (Iterator i = resultConfigParams.entrySet().iterator(); i.hasNext(); ) {
                Map.Entry e = (Map.Entry) i.next();
                if (! prohibitedResultParam.contains(e.getKey())) {
                    requestParameters.put(e.getKey().toString(),
                            e.getValue() == null ? "":
                                conditionalParse(e.getValue().toString(), invocation));
                }
            }
        }

        StringBuffer tmpLocation = new StringBuffer(actionMapper.getUriFromActionMapping(new ActionMapping(actionName, namespace, method, null)));
        UrlHelper.buildParametersString(requestParameters, tmpLocation, "&");

        setLocation(tmpLocation.toString());

        super.execute(invocation);
    
public voidsetActionName(java.lang.String actionName)
Sets the action name

param
actionName The name

        this.actionName = actionName;
    
public voidsetMethod(java.lang.String method)
Sets the method

param
method The method

        this.method = method;
    
public voidsetNamespace(java.lang.String namespace)
Sets the namespace

param
namespace The namespace

        this.namespace = namespace;