struts2获取表单提交数据的三种方法

(一)使用struts2中的ServletActionContext中的方法得到

HttpServletRequest request = ServletActionContext.getRequest();
		request.getParameter("username");
功能非常强大,可以get到request,response,pageContext,actionContext,servletContext,actionmapping和valueStack等等



(二)属性封装获取表单数据

public class UserAuthenAction extends ActionSupport {
	//通过属性封装操作
	//1、将变量的名称和表单输入项属性值一致
	//2、生成相应的setter和getter方法即可获得
	private String username;
	private String password;
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String execute() throws Exception {
		System.out.println(username);
		System.out.println(password);
		return NONE;
	}
}




(三)模型驱动获取表单数据

public class UserAction extends ActionSupport implements ModelDriven {
	//1、action实现ModelDriven接口
	//2、创建实体类User
	//3、复写接口中的getModel()方法,返回实体类
	private User user = new User();
	public User getModel() {		
		return user;
	}	
	public String execute() throws Exception {
		System.out.println(user.getUsername()+user.getPassword()+user.getAddress()+user.getSchool());
		return "loginfailed";
//		return "loginsuccess";
	}	
}





你可能感兴趣的:(Struts2学习)