【发布时间】:2015-04-20 21:13:53
【问题描述】:
我目前正在学习 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 和 annotation-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=txt}”而不是“无法验证此表单..”。
编辑:
这是我的类路径:
和 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>
【问题讨论】:
-
Circle{name=NomDuCercle} 这个名字是从哪里来的。超过 5 个字符!
-
愚蠢的我,我写了一个糟糕的例子!我更新了这个问题^^
标签: java spring hibernate spring-mvc bean-validation