【问题标题】:JSF : Better way to check for existence of <h:message for="id"/>JSF:检查 <h:message for="id"/> 是否存在的更好方法
【发布时间】:2011-05-30 04:28:34
【问题描述】:

我有一个表单,其中需要在输入元素下方显示验证错误消息。需要通过在错误消息和输入文本周围显示错误气泡来突出显示错误。

为此,我需要检查单个元素是否存在 h:messages。 我可以如下检查是否存在全局错误消息

<h:panelGroup rendered="#{not empty facesContext.messages}"> 
</h:panelGroup>

我如何检查特定客户 ID(比如名字)。所以像

faceContent.messages("creditCardNo")

我目前的一个解决方案是创建一个自定义解析器,但想知道是否有更好的解决方案。

【问题讨论】:

    标签: jsf jsf-2


    【解决方案1】:
    Iterator<FacesMessage> pkit = fctxt.getMessages(); // check all existing messages from the FacesContext         
    while(pkit.hasNext()){ 
        System.out.println(pkit.next().getDetail()); // get message text
        System.out.println(pkit.next().getSummary());
    }
    

    如果在 while 循环中找到您要查找的消息,您可以采取适当的措施

    【讨论】:

    • 采取适当的行动是这里的挑战。你会怎么做?
    【解决方案2】:

    我使用引导程序,也需要这个...通过创建自定义组件包装器来解决它。

    <composite:interface>
        <composite:attribute name="casoCustomFieldValue" required="true" />
        <composite:attribute name="casoOpen" default="true" />
    </composite:interface>
    
    <composite:implementation>
    
        <h:panelGroup styleClass="#{not empty facesContext.getMessageList(''.concat(cc.clientId).concat(':valortext')) ? 'form-group has-error' : 'form-group'}"
                      rendered="#{cc.attrs.casoCustomFieldValue.customField.fieldTypeId.fieldTypeId eq 'TEXT'}" >
            <p:inputText id="valortext" value="#{cc.attrs.casoCustomFieldValue.valor}" styleClass="form-control input-sm"
                     required="#{cc.attrs.casoCustomFieldValue.customField.required}" requiredMessage="Se necesita un valor." disabled="#{not cc.attrs.casoOpen}"/>
            <h:panelGroup rendered="#{not empty facesContext.getMessageList(''.concat(cc.clientId).concat(':valortext'))}" styleClass="help-block">
                <h:message for="valortext" />
            </h:panelGroup>
        </h:panelGroup>
    
    
    
    </composite:implementation>
    

    【讨论】:

      【解决方案3】:

      需要通过在错误消息周围显示错误气泡来突出显示错误...

      只需将&lt;h:message&gt; 与您定义所需样式的样式类一起使用。

      <h:inputText id="foo" />
      <h:message for="foo" styleClass="error" />
      

      .error {
          border: 1px solid red;
          background: pink;
      }
      

      ...以及输入文本。

      只需检查 UIInput#isValid() 是否为 true。从 JSF 2.0 开始,当前组件可通过隐式 EL 变量 #{component} 获得。

      <h:inputText styleClass="#{!component.valid ? 'error' : 'none'}" />
      

      至于实际关于检查是否有针对某个客户端ID 的消息的问题,那么您可能会发现this answer 很有趣。但我认为这不适用于您的特定情况。


      更新:根据 cmets,您似乎想要设置包含组件的样式而不是单独的组件。在这种情况下,请执行以下操作:

      <h:panelGroup styleClass="#{!foo.valid ? 'error' : 'none'}">
          <h:inputText id="foo" binding="#{foo}" />
          <h:message for="foo" />
      </h:panelGroup>
      

      【讨论】:

      • 感谢您指出 JSF2.0 中的 component.valid。我可以在我的代码中的其他地方使用它。但是,我需要在标签名称和输入框周围显示错误气泡。而 只会让我在输入框周围出现错误气泡
      猜你喜欢
      • 1970-01-01
      • 2015-10-31
      • 2016-02-29
      • 2019-10-23
      • 1970-01-01
      • 2011-12-29
      • 1970-01-01
      • 2014-10-23
      相关资源
      最近更新 更多