如何在Spring 3.x中使用Hibernate @Valid约束?

Bur*_*ede 3 java spring annotations constraints hibernate-validator

我正在使用简单的表单来验证像这样的字段.

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;


}
Run Code Online (Sandbox Code Playgroud)

我也在我的类路径上提供了getter和setter hibernate依赖项.但是我仍然没有得到如何验证简单形式,实际上没有那么多关于spring hibernate组合的文档.

@RequestMapping(value = "/contact", method = RequestMethod.POST)
public String add(@Valid Contact contact, BindingResult result) {
    ....
}
Run Code Online (Sandbox Code Playgroud)

除了原始的spring 3.x文档之外,你能解释一下还是给一些教程

Dir*_*irk 7

如果要使用@Valid批注来触发辅助bean的验证.然后它不是Hibernate注释它是来自验证API的javax.validation.Valid.

要让它运行,你需要两个:

在我的例子中,我使用自定义验证器(RegistrationValidator)而不是在支持bean中注释表单字段来进行验证.我需要为错误消息设置I18N密钥,这就是我必须用自己的替换Spring 3 MessageCodeResolver的原因.Spring 3中的原始版本总是尝试添加类型或字段名称,以便在第一种方式找不到时找到正确的密钥.

一个小例子:

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
         }

         ...
    }
}
Run Code Online (Sandbox Code Playgroud)

希望这会有所帮助.

顺便说一句.如果有人知道Bean Validation API的官方网页,请告诉... Thanx.

  • 如果您正在使用Maven,您可以获得org.hibernate:hibernate-validator:4.1.0-Final,其中也包含API. (2认同)

mlo*_*o55 7

我知道这个问题已经回答了...但是这里我的价值是0.02美元:-)我使用了Burak所指的相同例子,并且验证也没有被自动调用...我必须添加<mvc:annotation-driven /> 到我的应用程序上下文文件...(然后验证被触发).确保你还将mvc细节添加到模式声明......示例如下......

<?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>
Run Code Online (Sandbox Code Playgroud)