JSF EL expressions to check empty string in proper

2019-03-05 14:11发布

I have to check if my property file is empty for certain label based on that I have to render the element but even when the label is empty i still get the element displaying key:

           <h:panelGroup  rendered="#{not empty I18N['key_hint_message'] }">
          <h:outputLabel id="hint_label" value="#{I18N['key_label_hint']}
           "></h:outputLabel>
          <h:outputText value="#{I18N['key_hint_message']}" ></h:outputText>
          </h:panelGroup>

2条回答
叼着烟拽天下
2楼-- · 2019-03-05 14:46

JSF implementation displays by defaults missing resources as ???resource???, So you can use the fn:contains JSTL function in your rendered attribute like this:

 <h:panelGroup  rendered="${not fn:contains(I18N['key_hint_message'], '???')}">
      <h:outputLabel id="hint_label" value="#{I18N['key_label_hint']}"/>
      <h:outputText value="#{I18N['key_hint_message']}" />
 </h:panelGroup>
查看更多
Explosion°爆炸
3楼-- · 2019-03-05 14:48

You can use ResourceBundle#containsKey() for this.

<h:panelGroup rendered="#{I18N.containsKey('key_hint_message')}">
    <h:outputLabel value="#{I18N['key_label_hint']}" />
    <h:outputText value="#{I18N['key_hint_message']}" />
</h:panelGroup>

You'd better not rely on default format of missing keys as this can be overriden by a custom resource bundle resolver.

查看更多
登录 后发表回答