BindingResult总是假的Spring MVC

时间:2017-12-05 16:55:12

标签: spring jsp spring-mvc

我试图在我的mvc应用程序中实现spring验证框架。我的登录页面包含必填的用户名和密码。我在模型类中将它们定义为非null而不是空。如果它们留空,我希望spring将错误绑定到BindingResult对象。我不知道我做错了什么.BindingResult的hasError()方法总是在我的控制器类中返回false。

以下是我的登录控制器

public class LoginController {

@Autowired
private UserRoleService userRoleService;

@Autowired
private AuthenticationService authenticationService;

@RequestMapping(value = "/login", method=RequestMethod.GET)
public ModelAndView showLoginPage() {
    System.out.println("coming into LoginController!!");
    ModelAndView mav = new ModelAndView("login");
    mav.addObject("userModel", new UserInfo());
    return mav;
}

@RequestMapping(value = "/welcome", method=RequestMethod.POST)
public ModelAndView login(@Valid @ModelAttribute("userModel") UserInfo user, BindingResult bindingResult) {
    System.out.println("authenticate username and password -- invoke LDAP!!");
    System.out.println(user.getUsername() + user.getPassword());
    String userId = user.getUsername();
    String password = user.getPassword();

    if(bindingResult.hasErrors())
    {
        return new ModelAndView("login");
    }

    if(authenticationService.athenticate(userId,password)) {            
        List<String> roles = userRoleService.searchRolesByUserId(userId);           
        for(String role : roles)
            System.out.println("role is: "+role);
        ModelAndView mav = new ModelAndView("welcome");
        return mav;
    }
    else {
        ModelAndView mav = new ModelAndView("login");
        mav.addObject("loginFailed", "User Name or Password is incorrect");
        return mav;
    }
}
}

以下是我的模特课..

public class UserInfo{

@NotNull
@NotEmpty(message="User name cannot be empty")
private String username;

@NotNull
@NotEmpty(message="Password cannot be empty")
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;
}

}

我的春天xml有这些条目......

<bean id="viewResolver"
    class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
    <property name="prefix">
        <value>/views/</value>
    </property>
    <property name="suffix">
        <value>.jsp</value>
    </property>
</bean>
<mvc:resources mapping="/resources/**" location="/WEB-INF/resources/"/>
<mvc:annotation-driven />

以下是我的登录jsp页面..

<form:form class="form-signin" commandName="userModel" method="POST" action="welcome.do" id="loginform">

    <h2 class="form-signin-heading">Welcome to BPAY</h2>

    <div style="color: red">${loginFailed}</div>                                        

            <label for="inputEmail" class="sr-only">Username</label> 

            <form:input type="text" path="username" id="usernameId" class="form-control"
                placeholder="Username" required autofocus/>
            <form:errors path="username" cssClass="error"/>

            <label for="inputPassword" class="sr-only">Password</label> 

            <form:input type="password" id="passwordId" path="password" class="form-control"
                placeholder="Password" required/>
            <form:errors path="password" cssClass="error"/>

            <button class="btn btn-lg btn-primary btn-block" type="submit" value="OK">Login</button>                

    </form:form>

我的控制器类中的bindingResult.hasErrors()方法总是返回false。你能告诉我,我错过了什么?

pom要求输入如下..

<!-- Validation framework -->
    <dependency>
        <groupId>javax.validation</groupId>
        <artifactId>validation-api</artifactId>
        <version>1.1.0.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>5.3.5.Final</version>
    </dependency>

1 个答案:

答案 0 :(得分:1)

你应该使用这个依赖

`<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-validator</artifactId>
    <version>6.0.10.Final</version>
</dependency>`

并且移除依赖javax.validation

<dependency>
    <groupId>javax.validation</groupId>
    <artifactId>validation-api</artifactId>
    <version>1.1.0.Final</version>
</dependency>