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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1770075/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 17:53:36  来源:igfitidea点击:

How to user Hibernate @Valid constraint with Spring 3.x?

javaspringannotationsconstraintshibernate-validator

提问by Burak Dede

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.

我也在我的类路径上提供了 getter 和 setter 休眠依赖项。但是我仍然不知道如何验证简单的表单实际上没有太多关于 spring 休眠组合的文档。

@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

除了原始的 spring 3.x 文档外,您能否解释一下或提供一些教程

回答by Dirk

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.

如果您想使用 @Valid 注释来触发您的支持 bean 的验证。那么它不是 Hibernate 注释,而是来自验证 API 的 javax.validation.Valid 。

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.

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

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.

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

回答by mlo55

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...

我知道这个问题得到了回答......但无论如何,这是我价值 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>