1

I'm using JSF, and I have to load a bundle called Extra.

<f:loadBundle basename="com.ni.lib.extra.delivery.ExtraBundle" var="extra" />

Inside that extra variable, there's a value called downtime_notice. Now, if that value is NOT empty, I have to show a css segment with the text contained within the downtime_notice value. Something like this (of course this doesn't work):

if(extra.donwtime_notice!=''){
            <div class="pnx-msg pnx-msg-warning clearfix">
                <i class="pnx-msg-icon pnx-icon-msg-warning"/>
                <span class="pnx-msg-content"><h:outputText value="#{extra.downtime_notice}" escape="false"/></span>
            </div>
            </br>
}

I can use javascript, just in case. Any ideas? Thanks!

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Nacho321
  • 1,911
  • 7
  • 33
  • 55

1 Answers1

2

You can use <ui:fragment> or <h:panelGroup> to conditionally render content. You can use the empty keyword in EL to check if a variable is not null or empty. So, all with all this should do:

<ui:fragment rendered="#{not empty extra.donwtime_notice}">
    <div class="pnx-msg pnx-msg-warning clearfix">
        <i class="pnx-msg-icon pnx-icon-msg-warning"/>
        <span class="pnx-msg-content"><h:outputText value="#{extra.downtime_notice}" escape="false"/></span>
    </div>
</ui:fragment>

Or, using <h:panelGroup layout="block"> which renders a <div> already:

<h:panelGroup layout="block" styleClass="pnx-msg pnx-msg-warning clearfix" rendered="#{not empty extra.donwtime_notice}">
    <i class="pnx-msg-icon pnx-icon-msg-warning"/>
    <span class="pnx-msg-content"><h:outputText value="#{extra.downtime_notice}" escape="false"/></span>
</h:panelGroup>

Note that some may opt to use <f:verbatim> for the job, but this tag is deprecated since JSF2.

See also:


Unrelated to the concrete problem, the </br> tag is invalid HTML, so I omitted it form the examples.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Works perfectly. Just one more question, to avoid creating another thread... I'm trying to compare the downtime_notice value to a ' '. I've been trying to use something like rendered="#{extra.downtime_notice ne ' '}" but doesn't work. Any ideas? – Nacho321 Jan 17 '13 at 19:50
  • 1
    Properties file values are by default trimmed. So you need something else. Perhaps `_`? – BalusC Jan 17 '13 at 19:54
  • For some reason, it's returning an error. The code looks like this: . Maybe the ne clause doesn't work for the ui:fragment? – Nacho321 Jan 17 '13 at 20:01