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
The default .action
is configurable, just change the “struts.action.extension
” value to suit your need.
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;
}
}
I don’t understand why many Struts developers like to implement this Action interface, it better to extend the ActionSupport
.
3. ActionSupport
Support class, a common practice to provide default implementations of interfaces.
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 :
- Validation – Declared a
validate()
method and put the validation code inside. - 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"));
}
}
}
In most cases, you should extends this class for the ready convenient features, unless you have reason not to.
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.