Results Type in Struts2

After executing the Action class , the target view page will be executed based on configuration in struts.xml. The Srruts2 framework provides set of Result types like; chaining, dispatcher, redirect action, velocity and freemarket etc, that has to be configure in struts.xml for different kind of result. The result type is used to change the behaviour of view page to render the contents of action class.

The com.opensymphony.xwork2.Result interface implemented by all the result classes. The Result interface has a single method public void execute(ActionInvocation invocation) throws Exception.

The com.opensymphony.xwork2.config.entities.ResultConfig class stores the configuration information from struts.xml for each result configured with Action class.

The com.opensymphony.xwork2.config.entities.ResultTypeConfig class stores information about result type in struts.xml for each action class.

Default parameter :
To minimize configuration, Results can be configured with default value in the struts.xml configuration file. For example, here is a result defined in XML that uses a default parameter:


That is the equivalent to this:
<result name="success" type="dispatcher">
<param name="location">/Success.jsp</param>

Result configuration :
When method of action class completes, it returns a String value. This value of the String("success","input","error" etc ) is used to select a result element in struts.xml configuration file. The String value matched with an action mapping will often have a set of results representing different possible outcomes. A standard set of result tokens are defined by the ActionSupport base class.

Predefined result names in Action interface :
String SUCCESS = "success";
String NONE = "none";
String ERROR = "error";
String INPUT = "input";
String LOGIN = "login";

Configure multiple results : Please interpret below code as follows.
a) If method return "success" it forward to /hello/Result.jsp
b) If method return "error" it forward to /hello/Error.jsp
c) If method return "input" it forward to /hello/Input.jsp
d) if method return other than success,error and input then forward to /hello/Other.jsp page

<action name="Hello">
<result name="error">/hello/Error.jsp</result>
<result name="input">/hello/Input.jsp</result>
<result name="*">/hello/Other.jsp</result>

The Struts2 framework provides several implementations of the com.opensymphony.xwork2.Result interface, ready to use in your own applications.

There are eleven types of results in Struts2 :
  1. Dispatcher Result : Used for dispatch the request to other JSP page to include contents or forward contents. It is default result type.
  2. Chain Result : Used for Action Chaining
  3. FreeMarker Result : Used for FreeMarker integration
  4. Redirect Result : Used to redirect to another URL
  5. Redirect Action Result : Used to redirect to another action mapping
  6. Stream Result : Used to stream an InputStream back to the browser
  7. Velocity Result : Used for Velocity integration
  8. XSL Result : Used for XML/XSLT integration
  9. PlainText Result : Used to display the raw content of a particular page

Previous Next

Share your valuable comment

Name :
Email Id : [ We will not publish your email id ]
Your Comment : [ Maximum 200 chars ]
Enter Security code :