1. 程式人生 > >2 struts2 Action類的三種建立方式

2 struts2 Action類的三種建立方式

目前都在使用繼承ActionSupport類的方法,因為實現了很多方法。

1.直接建立Java類

package action;

/**
 * 測試action用類
 * action類必要條件:public修飾符 返回值String
 * 需要在strust.xml配置檔案中配置,才能使用此類中的方法。
 * @author Administrator
 *
 */
public class HelloAction {


    public String hello() {
        //業務程式碼
        System.out.println("成功執行HelloAction");
        //返回字串在strust.xml配置檔案中對應的action找到對應的result轉發或重定向
        return "retMsg";
    }
}

2.實現com.opensymphony.xwork2.Action介面建立action類

package action;

import com.opensymphony.xwork2.Action;

/**
 * 一般不使用
 * 實現com.opensymphony.xwork2.Action介面來寫action類
 * @author Administrator
 *
 */
public class ActionDemo1 implements Action{

    @Override
    public String execute() throws Exception {
        System.out.println("通過實action介面");
        return this.SUCCESS;
    }
}

3.繼承com.opensymphony.xwork2.ActionSupport類來實現

package action;

import com.opensymphony.xwork2.ActionSupport;


/**
 * 實現了execute外很多方法,一般都使用此方法來實現
 * 父類實現了execute方法返回SUCCESS,可重寫此方法。
 * @author Administrator
 *
 */
public class ActionDemo2 extends ActionSupport{

    @Override
    public String execute() throws Exception {

        System.out.println("通過繼承com.opensymphony.xwork2.ActionSupport實現。");
        return this.SUCCESS;
    }
}