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
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 :

  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"));
        }
    }
}

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值