即使字段不为空或没有任何空格,bindingResult.hasError()也会返回true。我想知道我在这里失踪了什么.--------------------------------------- --------------------
这是我的控制器
@RequestMapping(value = "/login",method = RequestMethod.POST)
public ModelAndView login(@ModelAttribute("user")User user, BindingResult bindingResult,
HttpServletRequest request, HttpServletResponse response,
@RequestParam("usernametxt") String username, @RequestParam("passwordtxt") String password)
{
try
{
// Using Spring ValidationUtils class to check for empty fields.
// This will add the error if any in the bindingResult object.
ValidationUtils.rejectIfEmptyOrWhitespace(bindingResult,"userName","usernametxt", "Username cannot be empty.");
ValidationUtils.rejectIfEmptyOrWhitespace(bindingResult,"password","passwordtxt", "Password should not be empty");
if (bindingResult.hasErrors())
{
//returning the errors on same page if any errors..
return new ModelAndView("/LoginPage", "user", user);
}
else
{
// If the user details is validated then redirecting the user to success page,
// else returning the error message on login page.
ServiceFacade facade = new ServiceFacadeImpl();
List<User> userList = facade.getAllUsers();
for(int index = 0; index < userList.size(); index++){
String uname = userList.get(index).getUserName();
String pword = userList.get(index).getPassword();
if(username.equals(uname) && password.equals(pword))
{
request.getSession().setAttribute("user", user);
//Creating a redirection view to success page. This will redirect to UsersController
RedirectView redirectView = new RedirectView("success.do", true);
return new ModelAndView(redirectView);
}
else
{
bindingResult.addError(new ObjectError("Invalid", "Invalid credentials. " +
"Username or Password is incorrect."));
//return new ModelAndView("login", "user", user);
}
}
}
} catch (Exception e) {
System.out.println("Exception in LoginController "+e.getMessage());
e.printStackTrace();
return new ModelAndView("/LoginPage", "user", user);
}
return null;
}
编辑1
USER BEAN
package com.nutsaboutcandywebproject.model;
import static javax.persistence.GenerationType.IDENTITY;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "users", catalog = "db_nutsaboutcandy")
public class User {
private int userId;
private String username;
private String firstname;
private String lastname;
private String password;
private String role;
private String address;
public User(){
}
public User(int userId, String username, String firstname,
String lastname, String password, String role, String address) {
super();
this.userId = userId;
this.username = username;
this.firstname = firstname;
this.lastname = lastname;
this.password = password;
this.role = role;
this.address = address;
}
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "user_id", unique = true, nullable = false)
public Integer getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
@Column(name = "username", nullable = false)
public String getUserName() {
return username;
}
public void setUserName(String username) {
this.username = username;
}
@Column(name = "firstname", nullable = false)
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
@Column(name = "lastname", nullable = false)
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
@Column(name = "password", nullable = false)
public String getPassword() {
return password;
}
public void setPassword(String password) {
if(password.length()>=6){
this.password = password;
}
else{
throw new IllegalArgumentException("Password length must be more than 6 characters");
}
}
@Column(name = "user_role", nullable = false)
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
@Column(name = "address", nullable = false)
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}