1.注入属性
直接注入属性:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
public String userName; public String getUserName() { return userName; } public void setUserName(String userName) { this .userName = userName; } @Override public String execute() throws Exception { // TODO Auto-generated method stub User 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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
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; } @Override public String execute() throws Exception { // TODO Auto-generated method stub UserDAO dao = new UserDAO(); HttpServletResponse response= ServletActionContext.getResponse(); PrintWriter out = response.getWriter(); if (dao.checkExists(user)) { out.print( "" ); } else { out.print( "" ); } return null ; } @Override public User getModel() { // TODO Auto-generated method stub return user; } |
注意:在使用方法1和3的时候前端及jsp代码部分无需需改,因为是直接调用的userName属性。
方法2需要将jquery中的username变为,user.username。