一:用户封装到JavaBean中-->nuc.sw.vo-->User.java
1 package nuc.sw.vo; 2 3 public class User { 4 private String username; 5 private String password; 6 public String getUsername() { 7 return username; 8 } 9 public void setUsername(String username) { 10 this.username = username; 11 } 12 public String getPassword() { 13 return password; 14 } 15 public void setPassword(String password) { 16 this.password = password; 17 } 18 }
二:修改LoginRegAction.java 实例化User类
一定要使用get set方法 ;否则会是空指针
1 private User user; 2 3 public User getUser() { 4 return user; 5 } 6 public void setUser(User user) { 7 this.user = user; 8 }
之前的username也要做相应的变化-->user.getUsername()
1 @Override 2 public String execute() throws Exception { 3 // public String LoginMethod() throws Exception { 4 // TODO Auto-generated method stub 5 //登录逻辑 6 if(user.getUsername().equals("zd")&&user.getPassword().equals("123")){ 7 ActionContext.getContext().getSession().put("user",user.getUsername()); 8 ActionContext.getContext().getSession().put("pass",user.getPassword()); 9 return SUCCESS; 10 } 11 else{ 12 13 ActionContext.getContext().put("error", "用户名或者密码错误"); 14 return ERROR; 15 } 16 17 } 18 @Override 19 public void validate() { 20 // TODO Auto-generated method stub 21 if(user.getUsername()==null||user.getUsername().trim().equals("")) 22 this.addFieldError("usernameError", "<font color='red'>用户名不能为空</font>"); 23 if(user.getPassword()==null||user.getPassword().trim().equals("")) 24 this.addFieldError("passwordError", "<font color='red'>密码不能为空</font>"); 25 26 }
三:修改login.jsp
1 用户名:<input type="text" name="user.username"><br> 2 密码:<input type="text" name="user.password"><br>
四:项目结构
五:运行结果
正确:
错误:
数据校验: