如何在百里香叶页面中显示来自 BindingResult.reject("message") 的消息

编程入门 行业动态 更新时间:2024-10-28 01:21:57
本文介绍了如何在百里香叶页面中显示来自 BindingResult.reject("message") 的消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我想在我的网页上显示 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屋!

更多推荐

[db:关键词]

本文发布于:2023-04-30 14:06:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1395675.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:百里   消息   页面   如何在   message

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!