【问题标题】:hibernate Validator not validating model classhibernate Validator 不验证模型类
【发布时间】:2018-07-15 12:32:02
【问题描述】:

这个问题已经被问过很多次了,即使在经历了所有的解决方案之后,我也无法让休眠验证器工作。

控制器类:-

@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”(min=3,max=7,message="user first name must have max length 7"),但是当我输入超过 7 的输入时,它仍然设置“user_first_name”。

输出:---

17:33:12.399 [http-bio-8080-exec-1] 调试 org.hibernate.validator.resourceloading.PlatformResourceBundleLocator - 找不到验证消息。 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] INFO 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] INFO com.xmith.sweb.HomeController - 验证不起作用 17:33:12.418 [http-bio-8080-exec-1] 信息 com.xmith.sweb.HomeController - setRegistration:退出

我错过了什么?

【问题讨论】:

  • 您在if(bindingResult.hasErrors()) 中打印“绑定成功”和“验证无效”。只是你的布尔逻辑是错误的。
  • 谢谢。这样一个蹩脚的错误,我的错。

标签: spring-mvc hibernate-validator


【解决方案1】:

正如 JB Nizet 指出的那样,我的布尔逻辑是错误的

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-12
    相关资源
    最近更新 更多