Struts 2 Actions
In this section we will learn about Struts 2 Actions, which is a fundamental concept in most of the web application frameworks. Struts 2 Action are the important concept in Struts 2 Framework. Actions are basically a unit of work that can be associated with a HTTP request.
About Struts Action Interface
In Struts 2 all actions may implement the com.opensymphony.xwork2.Action interface. It is not require to implement the Action interface and simple POJO classes can be used as action.
Struts 2 Action Interface field summary
Field | Description |
String ERROR | The action execution was a failure. Show an error view, possibly asking the user to retry entering data. |
String INPUT | The action execution require more input in order to succeed. This result is typically used if a form handling action has been executed so as to provide defaults for a form. The form associated with the handler should be shown to the end user. This result is also used if the given input params are invalid, meaning the user should try providing input again. |
String LOGIN | The action could not execute, since the user most was not logged in. The login view should be shown. |
String NONE | The action execution was successful but do not show a view. This is useful for actions that are handling the view in another fashion like redirect. |
String SUCCESS | The action execution was successful. Show result view to the end user. |
Different uses of Struts 2 Action
Struts 2 Action class can be used in many different ways. Here are some of the example.
In Struts2 actions can be used in multiple ways.
1. Single Result
In this case, the action would look like this:
class NewAction {
public void String execute() throws Exception {
return "success";
}
}
- This class is a simple POJO.
- It does not need to extend another class
- It does not need to implement any interface.
- It perform works with a single result always being returned.
It has one method named ?execute?. This name is the one used by convention. It can be named other than ?execute?, the only change needed would be in the actions configuration file.
Whatever the name of the method is, it will be expected to return a String result code. The actions configuration will match the result code the action returned to a specific result that will be rendered to the user. If needed, the method can also throw an exception.
The simplest configuration for the action looks like this:
<action name="new" class="net.rose.india.NewAction" >
<result>view.jsp</result>
</action>
The ?name? attribute gives the URL information to execute
the action.
2. Multiple Results
The next use is when the action returns multiple results depending on the outcome of the logic.
The class looks similar to the previous use:
class NewAction {
public void String execute() throws Exception {
if( newThings() ) {
return "success";
} else {
return "error";
}
}
}
Now, there can be two different results that can be returned,
So we need to configure what is to be sent back to the user for each case.
The configuration will become as compared with the previous one:
<action name=" new " class=" net.rose.india.NewAction " >
<result>view.jsp</result>
<result name="error">error.jsp</result>
</action>
This introduces a new ?name? attribute of the result node. In
fact, it has always been there. The value (as in the first result configuration) defaults to a value of ?success? if not provided by
the developer.