JSF定制h:selectOneMenu的验证消息(如果没有选择任何内容)

5
我有一个被枚举值填充的h:selectOneMenu,它工作得很好。唯一的问题是当没有选择有效值时,我不知道如何覆盖标准的JSF错误消息。错误消息总是bv:title: 'Choose' must be convertible to an enum from the enum that contains the constant 'Choose'.虽然我已经指定了requiredmessage和validatormessage(对InputText有效),但只显示标准JSF消息。
代码片段:
<h:selectOneMenu id="title" value="#{personBean.person.title}" required="true"
                  requiredMessage="ERROR"
                  validatorMessage="ERROR">
  <f:selectItem itemValue="Choose" />
  <f:selectItems value="#{personBean.titleOptions}" />
  <f:ajax event="blur" render="titleError" />
  <f:validateRequired/> 
</h:selectOneMenu> 
<h:message for="title" errorClass="invalid" id="titleError" />

我该如何覆盖标准验证器消息?或者更好的方法是,我能否创建JSF messages.properties的副本,并使用自定义错误消息(不想再次在自己的messages.properties中定义所有错误)?
1个回答

11

这不是一个“必填项”错误信息,而是一个“转换器”错误信息。当当前选择的项目既不符合预期类型也不在列表中的任何选项时,可能会出现此消息。只有当当前选择的项目为null时,才会显示必填消息。

您正在使用字符串值作为第一项。这不能转换为枚举。您需要将其设置为带有null项值的项目标签。

<f:selectItem itemLabel="Choose" itemValue="null" />

2
太棒了,完美运作 - 谢谢!!! 我用你的selectItem替换了原来的,并在selectOneMenu中定义了converterMessage="Please choose a value"。 - Wolkenarchitekt

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接