Working with Struts 2 actions
2015-09-17 10:14
471 查看
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.
In the
Now you can access the action via a suffix of
http://localhost:8080/Struts2Example/User/validateUser.action
The default
This interface is really simple, comes with 5 common used constant values :
I don’t understand why many Struts developers like to implement this Action interface, it better to extend the
The
The
Validation – Declared a
Text localization – Use
In most cases, you should extends this class for the ready convenient features, unless you have reason not to.
1. Action
Struts 2 actions don’t force you to implement any interface or extends class, it’s only required you to implement anexecute()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
actiontag and
classattribute. Define which result page should return to the user with
resulttag and the name of the action you can use to access this action class with
nameattribute.
<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
.actionextension.
http://localhost:8080/Struts2Example/User/validateUser.action
The default
.actionis 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,
inputand
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
ActionSupportclass 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@actionin 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 theActionSupportclass, it suits in most of the cases.
相关文章推荐
- Struts - 标签库
- struts1.0于struts2.0的比较
- Struts2中关于namespace的学习心得
- Struts2----自定义action的学习心得
- HTTP Status 404 - There is no Action mapped for namespace [/] and action name [login!welcome] associ
- struts2.0拦截器(实现未登录的用户不能访问系统的页面)(转)
- 软件开发疑惑解答
- struts2登陆拦截器
- struts2框架原理
- web.xml
- web.xml
- struts2框架原理
- struts2登陆拦截器
- liferay中配置Struts2
- struts2 拦截器和actioninvocation
- Struts2拦截器配置
- stuts2注解
- Struts 2国际化
- Struts2自定义类型转换器分为局部类型转换器和全局类型转换器
- Struts2 错误处理