gpt4 book ai didi

jsf - 如何自定义 JSF 转换消息 'must be a number consisting of one or more digits' ?

转载 作者:行者123 更新时间:2023-12-04 17:38:40 25 4
gpt4 key购买 nike

我有一个映射到 Long 的输入文本属性(property)。

private Long length;
<h:inputText value="#{bean.length}" />

当我在该输入文本中输入非数字时,出现以下转换错误:

myForm:myField: 'someText' must be a number consisting of one or more digits.



我想知道如何将此消息自定义为:

length must be a number greater than zero.

最佳答案

要么使用输入组件的 converterMessage属性:

<h:inputText converterMessage="length must be a number greater than zero" />

(不要忘记使用 <f:validateLongRange> 来防止用户输入负值并提供 validatorMessage !)

或者在类路径中创建一个属性文件来覆盖内置 JSF LongConverter 的默认消息:

javax.faces.converter.LongConverter.LONG = length must be a number greater than zero

并在 faces-config.xml注册为消息包:

<application>
<message-bundle>com.example.CustomMessages</message-bundle>
</application>

上面的例子假设文件名是 CustomMessages.properties并被放置在 com.example包裹。您可以命名并将其放置在您想要的任何位置。

您可以找到所有消息键的概述,例如 javax.faces.converter.LongConverter.LONG以及它们在 the JSF specification 的第 2.5.2.4 章中的默认值这也被复制粘贴在 this answer .

关于jsf - 如何自定义 JSF 转换消息 'must be a number consisting of one or more digits' ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9980037/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com