JSF Converter错误消息中的自定义变量 [英] Custom Variables in JSF Converter's Error Message
问题描述
我有一个表单页面,该页面具有一个接受日期的inputText字段.我们有一个转换器,可将文本框中的字符串转换为Date对象(即"2011-03-01"到java.util.Date("2011-03-01"))
I have a form page that has an inputText field that accepts a date. We have a converter that converts the string from the textbox into a Date object (ie. "2011-03-01" to java.util.Date("2011-03-01"") )
如果字符串不是日期,例如"123",则将显示验证错误消息,例如值(123)必须是日期".
If the string is not a date, like "123" then a validation error message will be displayed like "value (123) must be a date".
当前,在我的.properties文件中,我看到:
Currently, in my .properties file, I see:
javax.faces.converter.DateTimeConverter.DATE =值 ({0})必须是日期
javax.faces.converter.DateTimeConverter.DATE=value ({0}) must be a date
我需要通过确切指定哪个字段必须是日期来使此错误消息更加清晰. (因为表单上可能有多个日期文本字段.)
I need to make this error message more clear by specifying exactly which field must be a date. (As there could be more than one date text fields on the form).
我想将其更改为:
javax.faces.converter.DateTimeConverter.DATE = 值为"{1}"的字段"{0}"必须为 日期
javax.faces.converter.DateTimeConverter.DATE=The field "{0}" with value ({1}) must be a date
但是,我不确定JSF如何自动填充{0}和{1}.如何在JSF Converter错误消息中指定自己的变量?
However, I am unsure how JSF automatically fills in the {0} and {1}. How do I specify my own variables inside the JSF Converter error message?
注意:我已经添加了尝试创建自己的验证器(不要与转换器混淆),但是JSF框架似乎在其生命周期中进行了验证之前进行了转换.
Note: I have added tried creating my own validator (not to be confused with converter) but it seems that the JSF framework does conversion before validation in its lifecycle.
推荐答案
从JSF 1.2开始,使用converterMessage
属性替换整个消息,例如:
Starting from JSF 1.2, use the converterMessage
attribute to replace the entire message, such as:
<h:inputText value="#{user.dateOfBirth}" converterMessage="Format must be: yyyy-MM-dd">
<f:convertDateTime pattern="yyyy-MM-dd" />
</h:inputText>
否则,默认情况下,JSF在<h:message>
中显示_detail
消息.仅当您使用<h:message showDetail="false" showSummary="true">
时,才会显示与您的问题类似的那个.我不确定您使用的是哪个JSF版本,但是在我的JSF 2.0.3中,默认详细信息用于f:convertDateTime
如下:
Otherwise, JSF by default shows the _detail
message in the <h:message>
. Only when you use <h:message showDetail="false" showSummary="true">
then the one similar as in your question will be displayed. I'm not sure what JSF version you're using, but in my JSF 2.0.3 the default detail message for f:convertDateTime
is the following:
javax.faces.converter.DateTimeConverter.DATE_detail = {2}: ''{0}'' could not be understood as a date. Example: {1}
{2}
将替换为客户端ID或输入字段的label
属性(如果存在).
The {2}
will be substituted with the client ID or the label
attribute of the input field when present.
<h:inputText value="#{user.dateOfBirth}" label="Date of birth">
<f:convertDateTime pattern="yyyy-MM-dd" />
</h:inputText>
必须为要使用的DATE_detail
消息定义DATE
和DATE_detail
消息:
Both the DATE
and DATE_detail
message must be defined for the DATE_detail
message to be used:
javax.faces.converter.DateTimeConverter.DATE=Date format must be: dd/mm/yyyy
javax.faces.converter.DateTimeConverter.DATE_detail=Date format must be: dd/mm/yyyy
另请参阅:
See also:
这篇关于JSF Converter错误消息中的自定义变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!