gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 13:48:56 26 4
gpt4 key购买 nike

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

@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";
}

如果我现在输入无效的内容,我会在提交后得到“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 似乎注意到该对象无效,但它没有再次显示表单,因此用户可以修复他的错误。我做错了什么?

最佳答案

由于 the tutorial here 找到了答案.我必须更改我的方法签名:

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

到:

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

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

关于java - 无法使用 Spring Boot 和 Thymeleaf 进行验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30297719/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com