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.circuleo.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=NomDuCercle}" instead of "Can't validate this form..".
Aucun commentaire:
Enregistrer un commentaire