休眠验证器无法验证模型类

时间:2018-07-15 12:32:02

标签: spring-mvc hibernate-validator

这个问题已经问了很多遍,即使在经历了所有解决方案后,我也无法使休眠验证器正常工作。

  

控制器类:-

@RequestMapping(value={"/setReg"},method={RequestMethod.GET,RequestMethod.POST})
public ModelAndView setRegistration( @Valid @ModelAttribute("userDetails") UserDetails userDetails,BindingResult bindingResult){
logger.info("setRegistration :Entry");
if(bindingResult.hasErrors()){
logger.info("binding success"); 
logger.info(userDetails.getUser_first_name());
logger.info("validation not working");
}
else{
logger.info("binding failure"); 
}
logger.info("setRegistration :Exit");
return null;    
}
  

servlet上下文:-

<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
        <context:component-scan base-package="com.xmith.services"/>
        <context:component-scan base-package="com.xmith.dao"/>
        <context:component-scan base-package="com.xmith.models"/>
        <context:component-scan base-package="com.xmith.sweb" />
    <context:annotation-config/>

    <!-- Enables the Spring MVC @Controller programming model -->
    <annotation-driven />

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
    <resources mapping="/resources/**" location="/resources/" />

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
    <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".jsp" />
    </beans:bean>
  

依赖性:-

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

验证类别:-

public class UserDetails {
private String user_id;
@Size(min=3,max=7,message="user first name must have max length 7")
private String user_first_name;
private String user_last_name;
private String user_age;
private String user_email;
private String user_password;

注意:- 在上面的代码中,我试图验证“ user_first_name”(最小= 3,max = 7,message =“用户名必须具有最大长度7”),但是当我输入的输入大于7时,它仍设置为“ user_first_name”。 / p>

  

输出:---

17:33:12.399 [http-bio-8080-exec-1]调试org.hibernate.validator.resourceloading.PlatformResourceBundleLocator-找不到ValidationMessages。 17:33:12.401 [http-bio-8080-exec-1]调试org.hibernate.validator.resourceloading.PlatformResourceBundleLocator-未找到ContributorValidationMessages。 17:33:12.403 [http-bio-8080-exec-1]调试org.hibernate.validator.resourceloading.PlatformResourceBundleLocator-找到org.hibernate.validator.ValidationMessages。 17:33:12.417 [http-bio-8080-exec-1]信息com.xmith.sweb.HomeController-setRegistration:Entry 17:33:12.418 [http-bio-8080-exec-1]信息com.xmith.sweb.HomeController-绑定成功 17:33:12.418 [http-bio-8080-exec-1]信息com.xmith.sweb.HomeController- qwertyuiiii 17:33:12.418 [http-bio-8080-exec-1]信息com.xmith.sweb.HomeController-验证无效 17:33:12.418 [http-bio-8080-exec-1]信息com.xmith.sweb.HomeController-setRegistration:Exit

我在这里想念什么?

1 个答案:

答案 0 :(得分:0)

JB Nizet指出我的布尔逻辑是错误的