Thymeleaf返回Java异常消息,而不是我的自定义错误消息。我怎样才能让它归还我想要的东西?
我正在实现表单验证,并且在thymeleaf的错误消息中遇到了BigInteger验证问题。以下是我的属性批注:Thymeleaf返回Java异常消息,而不是我的自定义错误消息。我怎样才能让它归还我想要的东西?,java,spring,spring-boot,spring-mvc,thymeleaf,Java,Spring,Spring Boot,Spring Mvc,Thymeleaf,我正在实现表单验证,并且在thymeleaf的错误消息中遇到了BigInteger验证问题。以下是我的属性批注: @Digits(integer = 9, fraction = 0, message="Must be a positive integer") private BigInteger myInteger; 控制员: @PostMapping("/") public String whatever(@Valid @ModelAttrib
@Digits(integer = 9, fraction = 0, message="Must be a positive integer")
private BigInteger myInteger;
控制员:
@PostMapping("/")
public String whatever(@Valid @ModelAttribute Entity myEntity, BindingResult result) {
if (result.hasErrors()) {
return "index";
}
//TODO
return "index";
}
最后是HTML代码
<span th:if="${#fields.hasErrors('myEntity.myInteger')}"
th:errors="*{myEntity.myInteger}"></span>
现在,这对于我的其他BigDecimal变量来说很好,但是BigInteger会导致Thymeleaf显示一个
NumberFormatException
,而不是我的自定义消息“必须是一个正整数。”
,可能是因为我不熟悉的错误处理优先级。我尝试过寻找答案,但大多数答案都指向一些基于messages.properties
的解决方案,这些解决方案不在我的项目文件夹中。我需要做什么来确保显示我的自定义消息而不是NumberFormatException
?您只需在resources
文件夹中创建消息。属性
并添加typemissmatch.table.myInteger=必须是正整数
。其中“table”是您的实体名称,小写为显示您的请求,即您可以在资源文件夹中创建消息。属性并添加类型不匹配。table.myInteger=必须为正整数。其中,table
是您的表名,小写。@Seldo97非常感谢您!我以前试过这个,但它不起作用,因为我没有用小写输入,谢谢你特别指出它out@TypoTC我添加了答案,你可以将其标记为问题的解决方案。