如何自定义JSF转换消息“必须是一个或多个数字组成的数字"? [英] How to customize JSF conversion message 'must be a number consisting of one or more digits'?
问题描述
我有一个映射到Long
属性的输入文本.
I have an input text that is mapped to a Long
property.
private Long length;
<h:inputText value="#{bean.length}" />
当我在输入文本中输入非数字时,出现以下转换错误:
When I enter non-digits in that input text, I get the following conversion error:
myForm:myField:"someText"必须是由一个或多个数字组成的数字.
myForm:myField: 'someText' must be a number consisting of one or more digits.
我想知道如何将此消息自定义为:
I was wondering how to customize this message to:
长度必须是大于零的数字.
length must be a number greater than zero.
推荐答案
两者都使用输入组件的converterMessage
属性:
Either use the input component's converterMessage
attribute:
<h:inputText converterMessage="length must be a number greater than zero" />
(并且不要忘记使用<f:validateLongRange>
来防止用户输入负值并提供validatorMessage
!)
(and don't forget to use <f:validateLongRange>
to prevent users being able to enter negative values and supply a validatorMessage
!)
或在类路径中创建一个属性文件,该文件将覆盖内置JSF LongConverter
的默认消息:
Or create a properties file in the classpath which overrides the default message of the builtin JSF LongConverter
:
javax.faces.converter.LongConverter.LONG = length must be a number greater than zero
,并且已在faces-config.xml
中注册为消息束:
and is been registered as message bundle in faces-config.xml
:
<application>
<message-bundle>com.example.CustomMessages</message-bundle>
</application>
上面的示例假定文件名是CustomMessages.properties
,并放在com.example
程序包中.您可以将其命名并放置在任意位置.
The above example assumes that the file name is CustomMessages.properties
and is been placed in com.example
package. You can name and place it wherever you want.
您可以在
You can find an overview of all message keys like javax.faces.converter.LongConverter.LONG
and their default values in chapter 2.5.2.4 of the JSF specification which is also copypasted in this answer.
这篇关于如何自定义JSF转换消息“必须是一个或多个数字组成的数字"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!