Java 无法使用 Spring Boot 和 Thymeleaf 进行验证

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30297719/
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-08-11 09:25:18  来源:igfitidea点击:

Cannot get validation working with Spring Boot and Thymeleaf

javaspringvalidationspring-mvc

提问by Wim Deblauwe

I have a Spring Boot application (using version 1.2.3) with 1 controller that shows a form. This all works fine, but now I want to add validation. I have this method in my controller:

我有一个带有 1 个显示表单的控制器的 Spring Boot 应用程序(使用版本 1.2.3)。这一切正常,但现在我想添加验证。我的控制器中有这个方法:

@RequestMapping(value = "/licensing", method = RequestMethod.POST)
public String doRegistration( @Valid CustomerLicenseRegistration customerLicenseRegistration, Model model, BindingResult bindingResult )
{
    if( bindingResult.hasErrors())
    {
        logger.debug( "There are errors! {}", bindingResult );
        return "customer/license-registration";
    }
    logger.debug( "customerLicenseRegistration: " + customerLicenseRegistration );
    CustomerLicense customerLicense = m_licenseService.createCustomerLicense( customerLicenseRegistration );
    model.addAttribute( "customerLicense", customerLicense );
    return "customer/license-registration-done";
}

If I now type something invalid, I get the "Whitelabel error page" after submit and my breakpoint inside the method is never hit (If I remove the @Validannotation, the breakpoint does get hit). The error page shows:

如果我现在输入无效的内容,我会在提交后得到“Whitelabel 错误页面”,并且我的方法内的断点永远不会被命中(如果我删除@Valid注释,断点会被命中)。错误页面显示:

Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.

Mon May 18 09:42:27 CEST 2015
There was an unexpected error (type=Bad Request, status=400).
Validation failed for object='customerLicenseRegistration'. Error count: 1

Spring seems to notice that the object is not valid, but it does not show the form again so the user can fix his mistake. What am I doing wrong?

Spring 似乎注意到该对象无效,但它不会再次显示该表单,因此用户可以修复他的错误。我究竟做错了什么?

采纳答案by Wim Deblauwe

Found the answer due to the tutorial here. I have to change my method signature from:

由于教程here找到了答案。我必须更改我的方法签名:

public String doRegistration( @Valid CustomerLicenseRegistration customerLicenseRegistration, 
Model model, 
BindingResult bindingResult )

to:

到:

public String doRegistration( @Valid CustomerLicenseRegistration customerLicenseRegistration, 
BindingResult bindingResult, 
Model model )

Notice how the BindingResulthas to be immediatelyafter the object I have annotated with @Valid.

注意BindingResult必须在我用 注释的对象之后立即出现@Valid

回答by Abhishek Singh

In my case it was wrong input to the input box. Actually i entered "-"special character in the input boxwhich throws same error -Validation failed for object='events'. Error count: 5. I resolved it by entering numerical/Stringvalues.

在我的情况下,输入框输入错误。实际上,我在输入框中输入了“-”特殊字符,这会引发相同的错误 - object='events' 验证失败。错误计数:5。我通过输入数字/字符串值来解决它。