1.注入属性
直接注入属性:
public String userName;public String getUserName() {return userName;}public void setUserName(String userName) {this.userName = userName;}@Overridepublic String execute() throws Exception {// TODO Auto-generated method stubUser user = new User();user.setUserName(userName);//在这里需要将username接收到并且去使用。UserDAO dao = new UserDAO();HttpServletResponse response= ServletActionContext.getResponse();PrintWriter out = response.getWriter();if(dao.checkExists(user)){out.print("");}else{out.print("");}return null;}
2.Domain Model 这是一般常用的方式
这里就不重复说明,
3.ModelDriven
第三种方法不常用,只需要了解;
其过程分为4个步骤:
(1)action实现ModelDriven<User>接口
(2) 添加抽象方法
(3)定义并初始化一个模型
User user=new User();
(4)生成setter和getter
public class CheckUserAction extends ActionSupport implements ModelDriven<User>{private User user = new User();public User getUser() {return user;}public void setUser(User user) {this.user = user;}@Overridepublic String execute() throws Exception {// TODO Auto-generated method stubUserDAO dao = new UserDAO();HttpServletResponse response= ServletActionContext.getResponse();PrintWriter out = response.getWriter();if(dao.checkExists(user)){out.print("");}else{out.print("");}return null;}@Overridepublic User getModel() {// TODO Auto-generated method stubreturn user;}
注意:在使用方法1和3的时候前端及jsp代码部分无需需改,因为是直接调用的userName属性。
方法2需要将jquery中的username变为,user.username。
新闻热点
疑难解答