簡體   English   中英

Thymeleaf 返回 Java 異常消息而不是我的自定義錯誤消息。 我怎樣才能讓它返回我想要的?

[英]Thymeleaf returning Java Exception message instead of my custom error message. How can I make it return what I want?

我正在實施表單驗證並在 thymeleaf 的錯誤消息中遇到 BigInteger 驗證問題。 這是我的財產注釋:

@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而不是我的自定義消息"Must be a positive integer." ,大概是因為我不熟悉的錯誤處理優先級。 我曾嘗試尋找答案,但其中大多數將我定向到一些基於messages.properties的解決方案,該解決方案在我的項目文件夾中不存在。 我需要做什么來確保顯示我的自定義消息而不是NumberFormatException

您可以在resources文件夾中創建messages.properties並添加typeMismatch.table.myInteger = Must be a positive integer 其中“table”是小寫的實體名稱。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM