Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

jsf 2 - How to customize JSF conversion message 'must be a number consisting of one or more digits'?

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Either use the input component's converterMessage attribute:

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

(and don't forget to use <f:validateLongRange> to prevent users being able to enter negative values and supply a validatorMessage!)

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

and is been registered as message bundle in faces-config.xml:

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

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...