JSF 为 h :selectOneMenu (if nothing selected) 自定义验证消息

标签 jsf jsf-2

我有一个填充了枚举值的 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 中定义所有错误)?

最佳答案

这不是“必需的”错误消息。这是“转换器”错误消息。当当前选定的项目与预期的类型或列表中的任何选项都不匹配时,可能会出现这种情况。仅当当前选择的项目为 null 时才会出现所需的消息.

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

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

关于JSF 为 h :selectOneMenu (if nothing selected) 自定义验证消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2957182/

相关文章:

jsf-2 - 如何将 List<Integer> 值绑定(bind)到 JSF 中的 selectManyListbox

java - Glassfish容器安全认证——如何加盐(j_security_check)

jsf - 在 p :fileDownload and hide status when it is finished 开始时显示状态

jsf - 请求的资源不可用

java - primefaces 命令按钮操作不适用于级联下拉菜单

jsf - 如何覆盖默认文件上传 h :message in ICEfaces

hibernate - 无法在运行 mysql 的情况下获取 JPA

jsf-2 - 在 EL 中使用可变参数调用方法抛出 java.lang.IllegalArgumentException : wrong number of arguments

java - JSF commandButton 操作返回正确的值,但导航规则不适用

jsf - 在 p :calendar 上禁用特定日期