【问题标题】:Change label in custom error messages更改自定义错误消息中的标签
【发布时间】:2014-03-08 16:27:35
【问题描述】:

我想在我的 JSF 应用程序中显示自定义错误消息。为此,我定义了自己的 Messages.properties 文件并在 faces-config.xml 中声明了它的位置。

这是我的 Messages.properties 文件

javax.faces.component.UIInput.REQUIRED=Invalid input.
javax.faces.component.UIInput.REQUIRED_detail={0} is required.

这是我的 faces-config.xml 文件

<?xml version="1.0" encoding="UTF-8"?>

  <faces-config xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns /javaee/web-facesconfig_2_0.xsd"
version="2.0">
 <application>
   <message-bundle>com.edifixio.common.i18n.Messages</message-bundle>
       <resource-bundle>
        <base-name>com.edifixio.common.i18n.testBundle</base-name>
        <var>tb</var>
      </resource-bundle>
 </application>
</faces-config>

现在我有了 xhtml 页面

<ui:composition template="/WEB-INF/templates/globalTemplates.xhtml"
    xmlns="http://www.w3.org/1999/xhtml"
 xmlns:h="http://java.sun.com/jsf/html"
 xmlns:f="http://java.sun.com/jsf/core"
 xmlns:ui="http://java.sun.com/jsf/facelets">

<ui:define name="title">#{tb['REG_PAGE']}</ui:define>
<ui:define name="content">
   <h:form>
       <h:panelGrid columns="2">
           <h:outputLabel value="#{tb['EYN']}"/>
           <h:inputText required="true" id="name"/>
           <h:outputText value="" />
           <h:message for="name" style="color:red"/>
           <h:outputLabel value="#{tb['EYA']}"/>
           <h:inputText required="true" id="address"/>
           <h:outputText value="" />
           <h:message for="address" style="color:red"/>
           <h:outputLabel value="#{tb['UID']}"/>
           <h:inputText id="uid" required="true"/>
           <h:outputText value="" />
           <h:message for="uid" style="color:red;"/>
           <h:outputLabel value="#{tb['PWD']}"/>
           <h:inputSecret required="true" id="Password"/>
           <h:outputText value="" />
           <h:message for="Password" style="color:red"/>
      </h:panelGrid>
      <h:panelGrid>
           <h:commandButton action="#{regController.success}" value="Submit" />
      </h:panelGrid>
   </h:form>
</ui:define>

现在我的问题是,当点击提交按钮验证时,它会打印如下消息::

j_idt11:Password is required.

此消息来自空密码字段。看起来一切正常。但是,我想将消息显示为“需要密码”而不是“需要 j_idt11:密码”。

我知道 jsf 中有 requiredMessage 属性,使用它我可以很容易地实现这一点,但我不想使用这个 requiredMessage 属性,我只想按照上面的过程将“id”作为参数传递{ 0} 在messages.properties 文件中并打印消息。所以,总而言之,我的要求是显示只有 id 属性的消息,而不是像“j_idt11:Password is required”这样的复合 id。

我知道 jsf 在运行时会生成这个复合 id,那么,有没有办法在显示错误消息时从 id 部分消除这个“j_idt11”?

【问题讨论】:

    标签: jsf


    【解决方案1】:

    在h:inputText或h:inputSecret中添加label属性:

    <h:inputSecret id="password" value="#{bean.password}"
        required="true" label="#{tb['PWD']}" />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-08
      • 1970-01-01
      • 2012-12-14
      • 1970-01-01
      • 2018-08-12
      • 2014-12-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多