struts 中的创建Action的三种方法

时间:2021-01-12 14:32:05

1.对于直接创建类,不实现接口和继承任何的类

例如创建一个helloAction

package cn.lonecloud.control;

import com.opensymphony.xwork2.Action;
/**
*
* @author lonecloud
* @Date
* @version
* @Description
*/
public class HelloAction{ public String execute() throws Exception {
System.out.println("scd");
return SUCCESS;
}
public String test(){
System.out.println("test");
return ERROR;
} }

2.实现Action接口

package cn.lonecloud.control;

import com.opensymphony.xwork2.Action;
/**
*
* @author lonecloud
* @Date
* @version
* @Description
*/
public class HelloAction implements Action{ @Override
public String execute() throws Exception {
System.out.println("scd");
return SUCCESS;
}
public String test(){
System.out.println("test");
return ERROR;
} }

3.继承ActionSupport类(推荐使用,里面使用的好多的方法)

package cn.lonecloud.control;

import cn.lonecloud.domain.User;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven; /**
* 用于对登录拦截器
*
* @author lonecloud
*
*/
/**
* @author lonecloud
*
*/
public class LoginAction extends ActionSupport implements ModelDriven<User> {
/**
*ModelDriven创建一个User实例
*/
private User user=new User();
//实现序列化接口
private static final long serialVersionUID = 1L; /**
* 登录验证
* @return
*/
public String LoginUI() {
System.out.println(user.getPassword());
if (user.getUserName()==null||"".equals(user.getUserName())) {
this.addFieldError("userName12","用户名出错");
return "LoginUI";
}
return "LoginUI";
} /* (non-Javadoc)
* @see com.opensymphony.xwork2.ModelDriven#getModel()
* 该方法对于返回model
*/
@Override
public User getModel() {
// TODO Auto-generated method stub
return user;
}
}