Working with Struts 2 actions
In Struts 2, you’ll spend most of your time working with actions. The action class contains business logic, retrieve resource bundle, hold the data, validation, and select the view result page that should send back to the user. It’s the heart of the Struts 2, so you have to understand the basic concept of actions.
1. Action
Struts 2 actions don’t force you to implement any interface or extends class, it’s only required you to implement an execute() method that returns a string to indicate which result page should return.
package com.mkyong.user.action; public class LoginAction{ //business logic public String execute() { return "success"; } }
In the struts.xml, configure the action class with action tag and class attribute. Define which result page should return to the user with result tag and the name of the action you can use to access this action class with name attribute.
<package name="user" namespace="/User" extends="struts-default"> <action name="validateUser" class="com.mkyong.user.action.LoginAction"> <result name="success">pages/welcome.jsp</result> </action> <package>
Now you can access the action via a suffix of .action extension.
http://localhost:8080/Struts2Example/User/validateUser.action
2. Optional Action interface
Struts 2 comes with an optional action interface (com.opensymphony.xwork2.Action). By implements this interface, it bring some convenient benefits, see the source code :
package com.opensymphony.xwork2; public interface Action { public static final String SUCCESS = "success"; public static final String NONE = "none"; public static final String ERROR = "error"; public static final String INPUT = "input"; public static final String LOGIN = "login"; public String execute() throws Exception; }
This interface is really simple, comes with 5 common used constant values : success, error, none, input and logic. Now the action class can use the constant value directly.
package com.mkyong.user.action; import com.opensymphony.xwork2.Action; public class LoginAction{ //business logic public String execute() { return SUCCESS; } }
3. ActionSupport
The ActionSupport (com.opensymphony.xwork2.ActionSupport), a very powerful and convenience class that provides default implementation of few of the important interfaces :
public class ActionSupport implements Action, Validateable, ValidationAware, TextProvider, LocaleProvider, Serializable { ... }
The ActionSupport class give you the ability to do :
1. Validation – Declared a validate() method and put the validation code inside.
2. Text localization – Use GetText() method to get the message from resource bundle.
package com.mkyong.user.action; import com.opensymphony.xwork2.ActionSupport; public class LoginAction extends ActionSupport{ private String username; private String password; public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } //business logic public String execute() { return "SUCCESS"; } //simple validation public void validate(){ if("".equals(getUsername())){ addFieldError("username", getText("username.required")); } if("".equals(getPassword())){ addFieldError("password", getText("password.required")); } } }
4. Action annotation
Struts 2 has very good support for annotations, you can get rid of the XML file and replace with @action in your action class.
package com.mkyong.user.action; import org.apache.struts2.convention.annotation.Action; import org.apache.struts2.convention.annotation.Namespace; import org.apache.struts2.convention.annotation.Result; import org.apache.struts2.convention.annotation.ResultPath; import com.opensymphony.xwork2.ActionSupport; @Namespace("/User") @ResultPath(value="/") public class ValidateUserAction extends ActionSupport{ @Action(value="Welcome", results={ @Result(name="success",location="pages/welcome_user.jsp") }) public String execute() { return SUCCESS; } }
Conclusion
No brainer, just extends the ActionSupport class, it suits in most of the cases.
action classes are controller i.e they have the navigational code..they r goin to select the jsp page that shud b displayed to the user..n model(hibernate) is responsible for fetching the data from the database..
Hi having problem with the multiple button implementation.
I want to bring them in 1 row and implement. Please suggest on this
[...] the website (source): “Struts 2 actions don’t force you to implement any interface or extends class, it’s [...]
May i know difference between validatable and validationaware?..
Hi,
I have started to use Struts 2 and have some question. I read that Struts2 is great MVC framework, but you have wrote:
“The action class contains business logic, retrieve resource bundle, hold the data, validation, and select the view result page that should send back to the user.”
Now it’s seems that actions classes contain both – controller and model.
Action class is a controller class, often times, you should separate the model and controller in two classes.
P.S Try use JSF 2.0, the component reuse feature is much better than Struts 2.