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

查看:29
本文介绍了如何自定义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属性:

<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中注册为消息包:

<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.

您可以在 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天全站免登陆