【问题标题】:Primefaces custom validation message [duplicate]Primefaces自定义验证消息[重复]
【发布时间】:2016-02-29 04:48:20
【问题描述】:

如何在使用素面消息标签时显示自定义验证消息?

假设我有一个名为Description 的字段,我想得到这样的消息:

说明:请输入值。

这里的描述是字段名,所以我真正的要求是

formId:inputId:请输入值。

但目前我得到了这个:

描述:验证错误:值是必需的。

我的代码:

<p:messages id="message" />
<p:inputText id="updatedescription" value="#{equipTemplateBean.equipmentSpecificationsVO.description}" requiredMessage="Please enter Description"                                           required='true'                         
</p:inputText>

请建议如何做到这一点。

【问题讨论】:

  • 使问题更具可读性,并修复了一些小的语法错误
  • 好像你的equipTemplateBean.equipmentSpecificationsVO.description 的默认值是空的,试着给它一个值看看会发生什么
  • 这不是 PrimeFaces 消息,而是 jsf 消息。 stackoverflow.com/questions/10411773/…

标签: jsf jsf-2 primefaces


【解决方案1】:

请考虑我显示自定义错误消息“此字段是必需的”的最小示例。提交时为空:

page.xhtml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:f="http://xmlns.jcp.org/jsf/core"
      xmlns:p="http://primefaces.org/ui">
    <f:view>
        <h:head/>
        <h:body>
            <h:form>
                <p:inputText id="input"
                             required="true"
                             requiredMessage="This field is required."/>
                <p:message for="input"/>

                <p:commandButton process="input"
                                 update="@form"/>
            </h:form>
        </h:body>
    </f:view>
</html>

【讨论】:

    【解决方案2】:

    正如你在 mojarra repo 文件中看到的:jsf-api/src/main/java/javax/faces/Messages.properties,第 80 行,你得到的验证消息是由这个属性定义的:

    javax.faces.component.UIInput.REQUIRED={0}: Validation Error: Value is required.
    

    您可以创建自己的消息属性文件来覆盖 primefaces 的默认键,一个 put:

    javax.faces.component.UIInput.REQUIRED={0}: Please enter value.
    

    因为这是你想要的消息。

    要使用这个属性文件,配置JSF的上下文文件,添加applicationxml节点如下:

    <application>
      <message-bundle>path.to.your.PropertiesMessageFile</message-bundle>
    </application>
    

    希望对你有帮助!

    【讨论】:

      猜你喜欢
      • 2013-06-05
      • 1970-01-01
      • 2012-12-12
      • 1970-01-01
      • 2015-04-05
      • 2019-07-04
      • 2019-10-16
      相关资源
      最近更新 更多