如何自定义JSF转换消息“必须是一个或多个数字组成的数字"? [英] How to customize JSF conversion message 'must be a number consisting of one or more digits'?

查看:118
本文介绍了如何自定义JSF转换消息“必须是一个或多个数字组成的数字"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个映射到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.

您可以在javax.faces.converter.LongConverter.LONG)及其默认值的概述. "rel =" nofollow noreferrer> JSF规范,该规范也复制粘贴在

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屋!

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