How to user Hibernate @Valid constraint with Spring 3.x?
-
21-09-2019 - |
문제
I am working on simple form to validate fields like this one.
public class Contact {
@NotNull
@Max(64)
@Size(max=64)
private String name;
@NotNull
@Email
@Size(min=4)
private String mail;
@NotNull
@Size(max=300)
private String text;
}
I provide getter and setters hibernate dependencies on my classpath also.But i still do not get the how to validate simple form there is actually not so much documentation for spring hibernate combination.
@RequestMapping(value = "/contact", method = RequestMethod.POST)
public String add(@Valid Contact contact, BindingResult result) {
....
}
Could you explain it or give some tutorial , except original spring 3.x documentation
해결책
If you want to use the @Valid annotation to trigger the validation of your backing bean. Then it's not the Hibernate annotation it's javax.validation.Valid from the validation API.
To get it running you need both:
- Hibernate Validator 4.1.0 or higher (download it from here: http://sourceforge.net/projects/hibernate/files/hibernate-validator)
- Bean Validation API (download it from here: http://repository.jboss.org/maven2/javax/validation/validation-api/1.0.0.GA/
In my case I used a custom validator (RegistrationValidator) instead of annotating the form fields in the backing bean to do the validation. I need to set the I18N key's for the error messages that's the reason that I had to replace the Spring 3 MessageCodeResolver with my own. The original one from Spring 3 always tries to add type or field name to find the right key when it's not found in the first way.
A little example:
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
...
import javax.validation.Valid;
@Controller
public class RegistrationController
{
@InitBinder
protected void initBinder(WebDataBinder binder)
{
binder.setMessageCodesResolver(new MessageCodesResolver());
binder.setValidator(new RegistrationValidator());
}
@RequestMapping(value="/userRegistration.html", method = RequestMethod.POST)
public String processRegistrationForm(@Valid Registration registration, BindingResult result, HttpServletRequest request)
{
if(result.hasErrors())
{
return "registration"; // the name of the view
}
...
}
}
So hope this helps.
Btw. If someone knows the official webpage of the Bean Validation API, please tell... Thanx.
다른 팁
I know that this one is answered... but here's my $0.02 worth anyway :-) I had used the same example that Burak was referring to, and the validation was also not being automatically invoked... I had to add the <mvc:annotation-driven />
to my application context file... (then the validation was triggered). Make sure you also add the mvc details to the schema declaration... example follows...
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/mvc
">
<mvc:annotation-driven />
...
</beans>