如何在百里香叶页面中显示来自 BindingResult.reject("message") 的消息 [英] How to display message from BindingResult.reject("message") in thymeleaf page

查看:30
本文介绍了如何在百里香叶页面中显示来自 BindingResult.reject("message") 的消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的网页上显示 bindingresult.reject("message") 抛出的消息.

I want to show the message thrown by bindingresult.reject("message") on my web page.

  if(product.getQuantity()<saleDetailDto.getQuantity()){
                    bindingResult.reject("Not enough item in inventory");
                    return "purchase-detail/save-form";
   }

推荐答案

您需要确定拒绝哪个字段.

You need to determine for which field you reject.

rejectValue() 方法用于向BindingResult 对象.第一个参数标识错误与哪个字段相关联.第二个参数 是一个错误代码,它充当messages.properties 文件(或messages_en.properties 或messages_fr.properties 等,如果它们正在被使用)的消息键.rejectValue()的第三个参数表示回退默认消息,如果在资源包中找不到匹配的错误代码,则显示该消息.

The rejectValue() method is used to add a validation error to the BindingResult object. The first parameter identifies which field the error is associated with. The second parameter is an error code which acts a message key for the messages.properties file (or messages_en.properties or messages_fr.properties etc, if these are being used). The third parameter of rejectValue() represents the fallback default message, which is displayed if no matching error code is found in the resource bundle.

if(product.getQuantity()<saleDetailDto.getQuantity()){
    bindingResult.rejectValue("quantity", "error.quantity", "Not enough item in inventory!");
    return "purchase-detail/save-form";
}

要在html上显示,您可以执行以下操作.

To show it on html, you can do as follows.

<span th:if="${#fields.hasErrors('quantity')}" th:errors="*{quantity}">Error!</span>

这篇关于如何在百里香叶页面中显示来自 BindingResult.reject("message") 的消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆