无法使用 Spring Boot 和 Thymeleaf 进行验证
2022-09-01 16:29:56
我有一个Spring Boot应用程序(使用版本1.2.3),带有1个显示表单的控制器。这一切都很好,但现在我想添加验证。我的控制器中有这种方法:
@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似乎注意到该对象无效,但它不会再次显示表单,因此用户可以修复他的错误。我做错了什么?