By ErVeY

2010-08-09 17:05:36 8 Comments

I'm trying to use <c:if> to conditionally put a <h:outputLink> inside a <h:dataTable> when the state is finished.

<h:dataTable value="#{bean.items}" var="item" width="80%">
        <f:facet name="header">
            <h:outputText value="State" />

        <c:if test="#{item.state != 'Finish'}">
            <h:outputText value="Missing value" />
        <c:if test="#{item.state == 'Finish'}">
            <h:outputLink value="myLink">
                <h:outputText value="Value = #{item.state}" />

But this does not work, why is that and how can I fix it?


@BalusC 2010-08-09 17:41:58

JSTL tags are evaluated during building of the view, not during rendering of the view. You can visualize it as follows: Whenever a view tree get created for the first time, all JSTL tags are executed and the result is a view with only JSF components. Whenever a view tree get rendered, all JSF components get executed and the result is HTML. So: JSF+JSTL doesn't run in sync as you'd expect from the coding. JSTL runs from top to bottom first, hands the result to JSF and then it's JSF's turn to run from top to bottom again. This may lead to unexpected results in JSF iterating components like UIData because the row data (in your particular case the #{item} object) is not available while JSTL runs.

In a nutshell: Use JSTL to control flow of JSF component tree building. Use JSF to control flow of HTML output generation.

You want to use the rendered attribute here.

<h:outputText value="Missing value" rendered="#{item.state ne 'Finish'}" />
<h:outputLink value="myLink" rendered="#{item.state eq 'Finish'}">
    <h:outputText value="Value = #{item.state}" />

See also:

Related Questions

Sponsored Content

1 Answered Questions

[SOLVED] jstl <c:if> tags not working in jsp file, getting error in tomcat 7

2 Answered Questions

[SOLVED] JSTL c:forEach doesn't work inside a JSF h:dataTable

2 Answered Questions

Avoid evaluation of code inside c:if tag in JSTL

  • 2012-07-10 09:10:35
  • Frikazoid
  • 1330 View
  • 1 Score
  • 2 Answer
  • Tags:   jsp jsf jstl

1 Answered Questions

[SOLVED] Using JSTL with JSF 1.1

1 Answered Questions

[SOLVED] JSTL <c:if> tag not working in JSF 1.1

  • 2011-02-25 02:42:39
  • Ameya Thakur
  • 2945 View
  • 0 Score
  • 1 Answer
  • Tags:   jsp jsf jstl tomahawk

Sponsored Content