Results Type in Struts2After 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 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
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 :
- Dispatcher Result : Used for dispatch the request to other JSP page to include contents or forward contents. It is default result type.
- Chain Result : Used for Action Chaining
- FreeMarker Result : Used for FreeMarker integration
- Redirect Result : Used to redirect to another URL
- Redirect Action Result : Used to redirect to another action mapping
- Stream Result : Used to stream an InputStream back to the browser
- Velocity Result : Used for Velocity integration
- XSL Result : Used for XML/XSLT integration
- PlainText Result : Used to display the raw content of a particular page