struts 中的创建Action的三种方法

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

 

posted @ 2016-07-25 00:44  lonecloud  阅读(499)  评论(0编辑  收藏  举报
我的博客即将同步至 OSCHINA 社区,这是我的 OSCHINA ID:lonecloud,邀请大家一同入驻:https://www.oschina.net/sharing-plan/apply