FileDocCategorySizeDatePackage
StrutsResultSupport.javaAPI DocExample8609Mon Jul 23 13:26:38 BST 2007org.apache.struts2.dispatcher

StrutsResultSupport

public abstract class StrutsResultSupport extends Object implements org.apache.struts2.StrutsStatics, com.opensymphony.xwork2.Result
A base class for all Struts action execution results. The "location" param is the default parameter, meaning the most common usage of this result would be:

This class provides two common parameters for any subclass:

  • location - the location to go to after execution (could be a jsp page or another action). It can be parsed as per the rules definied in the {@link TextParseUtil#translateVariables(java.lang.String, com.opensymphony.xwork2.util.ValueStack) translateVariables} method
  • parse - true by default. If set to false, the location param will not be parsed for expressions
  • encode - false by default. If set to false, the location param will not be url encoded. This only have effect when parse is true
NOTE: The encode param will only have effect when parse is true

In the struts.xml configuration file, these would be included as:

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

or

<result name="success" type="redirect" >
<param name="location">foo.jsp?url=${myUrl}</param>
<param name="parse">true</param>
<param name="encode">true</param>
</result>

In the above case, myUrl will be parsed against Ognl Value Stack and then URL encoded.

or when using the default parameter feature

<result name="success" type="redirect">foo.jsp</result>

You should subclass this class if you're interested in adding more parameters or functionality to your Result. If you do subclass this class you will need to override {@link #doExecute(String, ActionInvocation)}.

Any custom result can be defined in struts.xml as:

<result-types>
...
<result-type name="myresult" class="com.foo.MyResult" />
</result-types>

Please see the {@link com.opensymphony.xwork2.Result} class for more info on Results in general.

see
com.opensymphony.xwork2.Result

Fields Summary
private static final Log
_log
public static final String
DEFAULT_PARAM
The default parameter
private boolean
parse
private boolean
encode
private String
location
private String
lastFinalLocation
Constructors Summary
public StrutsResultSupport()


      
        this(null, true, false);
    
public StrutsResultSupport(String location)

        this(location, true, false);
    
public StrutsResultSupport(String location, boolean parse, boolean encode)

        this.location = location;
        this.parse = parse;
        this.encode = encode;
    
Methods Summary
protected java.lang.StringconditionalParse(java.lang.String param, com.opensymphony.xwork2.ActionInvocation invocation)
Parses the parameter for OGNL expressions against the valuestack

param
param The parameter value
param
invocation The action invocation instance
return
The resulting string

        if (parse && param != null && invocation != null) {
            return TextParseUtil.translateVariables(param, invocation.getStack(),
                    new TextParseUtil.ParsedValueEvaluator() {
                        public Object evaluate(Object parsedValue) {
                            if (encode) {
                                if (parsedValue != null) {
                                    try {
                                        // use UTF-8 as this is the recommended encoding by W3C to
                                        // avoid incompatibilities.
                                        return URLEncoder.encode(parsedValue.toString(), "UTF-8");
                                    }
                                    catch(UnsupportedEncodingException e) {
                                        _log.warn("error while trying to encode ["+parsedValue+"]", e);
                                    }
                                }
                            }
                            return parsedValue;
                        }
            });
        } else {
            return param;
        }
    
protected abstract voiddoExecute(java.lang.String finalLocation, com.opensymphony.xwork2.ActionInvocation invocation)
Executes the result given a final location (jsp page, action, etc) and the action invocation (the state in which the action was executed). Subclasses must implement this class to handle custom logic for result handling.

param
finalLocation the location (jsp page, action, etc) to go to.
param
invocation the execution state of the action.
throws
Exception if an error occurs while executing the result.

public voidexecute(com.opensymphony.xwork2.ActionInvocation invocation)
Implementation of the execute method from the Result interface. This will call the abstract method {@link #doExecute(String, ActionInvocation)} after optionally evaluating the location as an OGNL evaluation.

param
invocation the execution state of the action.
throws
Exception if an error occurs while executing the result.

        lastFinalLocation = conditionalParse(location, invocation);
        doExecute(lastFinalLocation, invocation);
    
public java.lang.StringgetLastFinalLocation()
Returns the last parsed and encoded location value

        return lastFinalLocation;
    
public voidsetEncode(boolean encode)
Set encode to true to indicate that the location should be url encoded. This is set to true by default

param
encode true if the location parameter should be url encode, false otherwise.

        this.encode = encode;
    
public voidsetLocation(java.lang.String location)
The location to go to after action execution. This could be a JSP page or another action. The location can contain OGNL expressions which will be evaulated if the parse parameter is set to true.

param
location the location to go to after action execution.
see
#setParse(boolean)

        this.location = location;
    
public voidsetParse(boolean parse)
Set parse to true to indicate that the location should be parsed as an OGNL expression. This is set to true by default.

param
parse true if the location parameter is an OGNL expression, false otherwise.

        this.parse = parse;