1.直接在对应的action层设置jsp页面内的同名属性
注意要添加getset方法
public class UserAction extends ActionSupport{ private UserService userService; public void setUserService(UserService userService) { this.userService = userService; } 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 login() throws Exception{ if(userService.checkUser(username,password)){ return "success"; } else return "error"; } }
<body> <form action="login.action" method="post"> 用户名:<input name="username" type="text" /> 密码:<input name="password" type="password" /> <input type="submit" value="提交"/> </form>
**2.将要接收的参数抽象到一个实体类 通过在action中使用实体类作为成员域 并且改jsp页面的参数为
成员域名.其属性名**
注意要添加实体类成员域的getset方法
public class UserAction extends ActionSupport{ private UserService userService; public void setUserService(UserService userService) { this.userService = userService; } private User user; public User getUser() { return user; } public void setUser(User user) { this.user = user; } public String login() throws Exception{ if(userService.checkUser(user.getUsername(),user.getPassword())){ return "success"; } else return "error"; } }
<form action="login.action" method="post"> 用户名:<input name="user.username" type="text" /> 密码:<input name="password" type="user.password" /> <input type="submit" value="提交"/> </form>
3.使用ModelDriver<实体类>接口
基本在第二种上面进行改进了 记得实现接口方法 改返回值为实体类成员域
注意实体类成员域要new 然后在JSP页面直接用属性名 不用user.username直接username
public class UserAction extends ActionSupport implements ModelDriven<User>{ private UserService userService; public void setUserService(UserService userService) { this.userService = userService; } private User user=new User(); public String login() throws Exception{ if(userService.checkUser(user.getUsername(),user.getPassword())){ return "success"; } else return "error"; } @Override public User getModel() { return user; } }
<form action="login.action" method="post"> 用户名:<input name="getUsername()" type="text" /> 密码:<input name="password" type="getPassword()" /> <input type="submit" value="提交"/> </form>