English 中文(简体)
How to user Hibernate @Valid constraint with Spring 3.x?
原标题:

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:

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>




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签