I'm currently learning spring but I'm stuck with the validation annotation that don't works with my bean. I really don't understand what missing and I would need a Hand :)
I have a controller :
@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);
}
}
And a bean :
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;
}
}
I configured the 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>
my prject looks like that :
the *-context.xml have the component-scan and anotation-config tags :
<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>
I have all the external libraries (hibernate, hibernate-api, javax.validation) and no error on the run time... but when I fill the field "name" wit less than 5 characters, I always get "Created new circle : Circle{name=txt}" instead of "Can't validate this form..".
EDIT :
Here is my classpath :
and the 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>
Providing the list of your dependencies and the circles-servlet.xml would give a full context to your question.
Nevertheless, for what I see there could be only two things missing. First make sure that you have the validation provider such as hibernate-validator
on your classpath, second make sure that you have
<mvc:annotation-driven />
element in your circles-servlet.xml it provides support for enabling validation against parameter objects of your controller annotated with @Valid
UPDATE after comment
the bean validation has an updated specification, so you should align your dependencies in the following manner
hibernate-validator-5.x.x
validation-api-1.1.x
which will implement a JSR-349
OR
hibernate-validator-4.x.x
validation-api-1.0.x.
which implements JSR-303
Your issue from the comment means that you've most likely mixed the dependency, so used hibernate-validator-5.xx with validation-api-1.0.x or missed it the other way around
See the very bottom of this page:
http://docs.spring.io/spring/docs/current/spring-framework-reference/html/validation.html
Add the following to you spring config:
<!-- JSR-303/JSR-349 support will be detected on classpath and enabled automatically -->
<mvc:annotation-driven/>
将@RequestBody注释与@Valid注释一起使用
public ModelAndView createCircle(@Valid @RequestBody Circle circle, BindingResult res) {
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.