hibernate Validator 未验证模型 class
hibernate Validator not validating model class
这个问题已经被问过很多次了,即使在完成了所有的解决方案之后我还是无法让休眠验证器工作。
Controller class:-
@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-context:-
<!-- 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:-
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.4.1.Final</version>
</dependency>
validation class:-
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".
output:---
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
我在这里错过了什么?
正如 JB Nizet 所指出的那样,我的布尔逻辑是错误的
这个问题已经被问过很多次了,即使在完成了所有的解决方案之后我还是无法让休眠验证器工作。
Controller class:-
@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-context:-
<!-- 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:-
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.4.1.Final</version>
</dependency>
validation class:-
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".
output:---
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
我在这里错过了什么?
正如 JB Nizet 所指出的那样,我的布尔逻辑是错误的