Struts2中Action类的三种写法

时间:2023-03-09 10:06:36
Struts2中Action类的三种写法

一.普通的POJO类(没有继承没有实现)-基本不使用

POJO(Plain Ordinary Java Object)简单的Java对象,实际就是普通JavaBeans,是为了避免和EJB混淆所创造的简称。...
Struts2中Action类的三种写法
public class DemoAction1 {

    public String execute(){

        System.out.println("DemoAction1是普通的POJO类...");

        return null;
}
}
Struts2中Action类的三种写法
        <!-- 普通的POJO类 -->
<action name="action1" class="com.struts2.web.action2.DemoAction1"/>

Struts2中Action类的三种写法

Struts2中Action类的三种写法

  基本不使用

二.实现Action接口-基本不使用

Struts2中Action类的三种写法
import com.opensymphony.xwork2.Action;

/**
* action类的编写2:实现action接口
* @author NEWHOM
*
*/
public class DemoAction2 implements Action { @Override
public String execute() throws Exception {
// TODO Auto-generated method stub System.out.println("DemoAction2是一个实现了Action接口的类..."); return null;
} }
Struts2中Action类的三种写法
        <!-- 实现了Action接口 -->
<action name="action2" class="com.struts2.web.action2.DemoAction2" />

Struts2中Action类的三种写法

Struts2中Action类的三种写法

* Action接口中定义了5个常量,5个常量的值对应的是5个逻辑视图跳转页面(跳转的页面还是需要自己来配置),还定义了一个方法,execute方法。
* 5个逻辑视图的常量
* SUCCESS -- 成功.
* INPUT -- 用于数据表单校验.如果校验失败,跳转INPUT视图.
* LOGIN -- 登录.
* ERROR -- 错误.
* NONE -- 页面不转向.

  基本不使用

三.继承ActionSupport类-经常使用

Struts2中Action类的三种写法
import com.opensymphony.xwork2.ActionSupport;

/**
* action编写3:继承ActionSupport类
* @author NEWHOM
*
*/
public class DemoAction3 extends ActionSupport { private static final long serialVersionUID = 1L; @Override
public String execute() throws Exception {
// TODO Auto-generated method stub System.out.println("DemoAction3是一个继承了ActionSupport的类..."); return null;
} }
Struts2中Action类的三种写法
        <!-- 继承了ActionSupport类 -->
<action name="action3" class="com.struts2.web.action2.DemoAction3"/>

Struts2中Action类的三种写法

Struts2中Action类的三种写法

  ActionSupport本身继承了许多的类,利于编写代码

Struts2中Action类的三种写法

  开发中经常使用这种方式

分类: Struts2