struts2 - 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;
    }
}

猜你喜欢

转载自blog.csdn.net/alexzt/article/details/82799418
今日推荐