Bean 验证不起作用
Bean Validation does't work
我目前正在学习 spring,但我受困于不适用于我的 bean 的验证注释。我真的不明白缺少什么,我需要一只手 :)
我有一个控制器:
@Controller
public class CirclesController {
@RequestMapping(value = "/createCircle", method = RequestMethod.POST)
public ModelAndView createCircle(@Valid Circle circle, BindingResult res) {
if (res.hasErrors()) {
System.out.println("Can't validate this form..");
else
System.out.println("Created new circle : " + circle);
}
}
还有一颗豆子:
public class Circle {
@Size(min = 5) // This is what I try to validate
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
我配置了web.xml
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:conf/dao-context.xml
classpath:conf/services-context.xml
classpath:conf/beans-context.xml
</param-value>
</context-param>
我的项目看起来像这样:
*-context.xml 有 component-scan 和 anotation-config 标签:
<context:component-scan base-package="com.test.app.[package-name]">
</context:component-scan>
<context:annotation-config></context:annotation-config>
<tx:annotation-driven></tx:annotation-driven>
我拥有所有外部库(hibernate、hibernate-api、javax.validation)并且 运行 时间没有错误...
但是当我用少于 5 个字符填写 "name" 字段时,我总是得到 "Created new circle : Circle{name=txt}" 而不是 "Can't validate this form..".
编辑:
这是我的类路径:
和 servlet-context.xml :
<context:component-scan base-package="com.test.app.controllers"></context:component-scan>
<mvc:annotation-driven></mvc:annotation-driven>
<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsps/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
将@RequestBody 注解与@Valid 注解一起使用
public ModelAndView createCircle(@Valid @RequestBody Circle circle, BindingResult res) {
查看本页底部:
http://docs.spring.io/spring/docs/current/spring-framework-reference/html/validation.html
添加以下内容给你spring配置:
<!-- JSR-303/JSR-349 support will be detected on classpath and enabled automatically -->
<mvc:annotation-driven/>
提供您的依赖项列表和 circles-servlet.xml 将为您的问题提供完整的上下文。
尽管如此,就我所见,可能只缺少两件事。首先确保您的类路径上有验证提供程序,例如 hibernate-validator
,其次确保您有
<mvc:annotation-driven />
你的 circles-servlet.xml 中的元素它支持对你的控制器的参数对象进行验证,并用 @Valid[=35= 注释]
评论后更新
bean 验证有一个更新的规范,所以你应该按照以下方式调整你的依赖关系
hibernate-validator-5.x.x
validation-api-1.1.x
这将实现 JSR-349
或
hibernate-validator-4.x.x
validation-api-1.0.x.
它实现了 JSR-303
你在评论中的问题意味着你很可能混合了依赖关系,所以使用 hibernate-validator-5.x.x 和 validation-api-1.0.x 或错过反过来
我目前正在学习 spring,但我受困于不适用于我的 bean 的验证注释。我真的不明白缺少什么,我需要一只手 :)
我有一个控制器:
@Controller
public class CirclesController {
@RequestMapping(value = "/createCircle", method = RequestMethod.POST)
public ModelAndView createCircle(@Valid Circle circle, BindingResult res) {
if (res.hasErrors()) {
System.out.println("Can't validate this form..");
else
System.out.println("Created new circle : " + circle);
}
}
还有一颗豆子:
public class Circle {
@Size(min = 5) // This is what I try to validate
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
我配置了web.xml
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:conf/dao-context.xml
classpath:conf/services-context.xml
classpath:conf/beans-context.xml
</param-value>
</context-param>
我的项目看起来像这样:
*-context.xml 有 component-scan 和 anotation-config 标签:
<context:component-scan base-package="com.test.app.[package-name]">
</context:component-scan>
<context:annotation-config></context:annotation-config>
<tx:annotation-driven></tx:annotation-driven>
我拥有所有外部库(hibernate、hibernate-api、javax.validation)并且 运行 时间没有错误... 但是当我用少于 5 个字符填写 "name" 字段时,我总是得到 "Created new circle : Circle{name=txt}" 而不是 "Can't validate this form..".
编辑:
这是我的类路径:
和 servlet-context.xml :
<context:component-scan base-package="com.test.app.controllers"></context:component-scan>
<mvc:annotation-driven></mvc:annotation-driven>
<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsps/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
将@RequestBody 注解与@Valid 注解一起使用
public ModelAndView createCircle(@Valid @RequestBody Circle circle, BindingResult res) {
查看本页底部:
http://docs.spring.io/spring/docs/current/spring-framework-reference/html/validation.html
添加以下内容给你spring配置:
<!-- JSR-303/JSR-349 support will be detected on classpath and enabled automatically -->
<mvc:annotation-driven/>
提供您的依赖项列表和 circles-servlet.xml 将为您的问题提供完整的上下文。
尽管如此,就我所见,可能只缺少两件事。首先确保您的类路径上有验证提供程序,例如 hibernate-validator
,其次确保您有
<mvc:annotation-driven />
你的 circles-servlet.xml 中的元素它支持对你的控制器的参数对象进行验证,并用 @Valid[=35= 注释]
评论后更新
bean 验证有一个更新的规范,所以你应该按照以下方式调整你的依赖关系
hibernate-validator-5.x.x
validation-api-1.1.x
这将实现 JSR-349
或
hibernate-validator-4.x.x
validation-api-1.0.x.
它实现了 JSR-303
你在评论中的问题意味着你很可能混合了依赖关系,所以使用 hibernate-validator-5.x.x 和 validation-api-1.0.x 或错过反过来