17

I can access my variable stored in a backing bean from my JSF2 page, so things like

<h:outputText value="#{myBean.myValue}"/>

work, and the value (which is an int btw.) prints okay. However, when trying to use this value in conditional expressions within c:if and/or c:when tags it never equals anything... so:

<c:if test="#{myBean.myValue == 1}">
    <c:set var="myVar" value="true"/>
</c:if>

<c:choose>
    <c:when test="#{myBean.myValue > 1}">
        <c:set var="myVar" value="true"/>
    </c:when>
</c:choose>

or even

#{myBean.myValue eq '1'} 

or

#{myBean.myValue == '1'}

will never evaluate to true, even if the value is indeed 1 or >1.

Annoyingly the comparison works in the rendered attribute of a tag! So this:

<h:outputText value="whatever" rendered="#{myBean.myValue == 1}"/>

is fine!

What is going on?

UPDATE:

This works:

public int getMyValue() {
    return 1;
}

but this does not:

@Column(name = "orderstatus")
public int getOrderStatus() {
return orderStatus;
}

The int's value is printed correctly, but 1 == 1 is false.

Are entity beans handled in a special way? Can I use them for displaying their value on the UI?

UPDATE2:

<h:outputText value="#{order.adcOrderStatus.orderStatus.class}"/>

prints java.lang.Integer.

UPDATE3:

Here is the full code:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:c="http://java.sun.com/jsp/jstl/core"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:cc="http://java.sun.com/jsf/composite">
    <cc:interface>
        <cc:attribute name="orderStatus" required="true"/>
    </cc:interface>
    <cc:implementation>
        <c:choose>
            <c:when test="#{cc.attrs.orderStatus == 1}" >
                <c:set var="unknownStatus" value="false"/>
            </c:when>
            <c:when test="#{cc.attrs.orderStatus == 2}" >
                <c:set var="unknownStatus" value="false"/>
            </c:when>
            <c:when test="#{cc.attrs.orderStatus == 3}" >
                <c:set var="unknownStatus" value="false"/>
            </c:when>
            <c:when test="#{cc.attrs.orderStatus == 99}" >
                <c:set var="unknownStatus" value="false"/>
            </c:when>
            <c:otherwise>
                <c:set var="unknownStatus" value="true"/>
            </c:otherwise>
        </c:choose>
        <h:graphicImage url="#{resource['img/icons/flag_yellow.png']}" rendered="#{cc.attrs.orderStatus == 1}"/>
        <h:outputText value="Created" rendered="#{cc.attrs.orderStatus == 1}"/>
        <h:graphicImage url="#{resource['img/icons/flag_orange.png']}" rendered="#{cc.attrs.orderStatus == 2}"/>
        <h:outputText value="Stopped" rendered="#{cc.attrs.orderStatus == 2}"/>
        <h:graphicImage url="#{resource['img/icons/flag_green.png']}" rendered="#{cc.attrs.orderStatus == 3}"/>
        <h:outputText value="Active" rendered="#{cc.attrs.orderStatus == 3}"/>
        <h:graphicImage url="#{resource['img/icons/flag_red.png']}" rendered="#{cc.attrs.orderStatus == 99}"/>
        <h:outputText value="Failed" rendered="#{cc.attrs.orderStatus == 99}"/>
        <h:graphicImage url="#{resource['img/icons/question_mark.png']}" rendered="#{unknownStatus}"/>
        <h:outputText value="Unknown" rendered="#{unknownStatus}"/>
    </cc:implementation>
</html>

It works when called with an int value. But this doesn't work:

    <p:dataTable value="#{cc.attrs.orders}" var="order">
        <p:column>
            <f:facet name="header">
                <h:outputText value="Status"/>
            </f:facet>
            <mytag:orderStatus orderStatus="#{order.adcOrderStatus.orderStatus}"/>
        </p:column>
    </p:dataTable>

This displays the correct value:

<h:outputText value="#{order.adcOrderStatus.orderStatus.class}"/>
egbokul
  • 3,944
  • 7
  • 36
  • 54

4 Answers4

28

Did you declare JSTL core taglib as follows?

<html xmlns:c="http://java.sun.com/jsp/jstl/core">

If not or incorrectly declared, they it simply won't be parsed and end up plain vanilla in generated HTML output. You can confirm this by opening the page in browser, rightlicking it and choosing View Source. You should not see any JSTL tag in there.


Update: as per your update, it's related to the fact that JSTL runs during build time of the view and JSF during render time of the view. In your particular case, this all would fail if #{cc.attrs.orderStatus} only available during render time of the view. For example, when it represents the currently iterated item of an iterating component, such as <h:dataTable>.

You'd better rewrite the composite component as follows to use the rendered attribute instead:

<h:panelGroup rendered="#{cc.attrs.orderStatus == 1}">
    <h:graphicImage url="#{resource['img/icons/flag_yellow.png']}" /> Created
</h:panelGroup>
<h:panelGroup rendered="#{cc.attrs.orderStatus == 2}">
    <h:graphicImage url="#{resource['img/icons/flag_orange.png']}" /> Stopped
</h:panelGroup>
<h:panelGroup rendered="#{cc.attrs.orderStatus == 3}">
    <h:graphicImage url="#{resource['img/icons/flag_green.png']}" /> Active
</h:panelGroup>
<h:panelGroup rendered="#{cc.attrs.orderStatus == 99}">
    <h:graphicImage url="#{resource['img/icons/flag_red.png']}"  /> Failed
</h:panelGroup>
<h:panelGroup rendered="#{cc.attrs.orderStatus != 1 && cc.attrs.orderStatus != 2 && cc.attrs.orderStatus != 3 && cc.attrs.orderStatus != 99}">
    <h:graphicImage url="#{resource['img/icons/question_mark.png']}" /> Unknown
</h:panelGroup>

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • The taglib must be correct, because Netbeans would complain otherwise. And c:otherwise is processed... – egbokul Jul 29 '10 at 12:29
  • Are you using JSF 2.0 on JSP or Facelets? – BalusC Jul 29 '10 at 12:31
  • JSF 2.0 and Facelets. No JSP. – egbokul Jul 29 '10 at 12:38
  • That was an informative comment, thank you. I originally wished to avoid using the rendered attribute though... – egbokul Jul 29 '10 at 13:25
  • As a note for future readers he *is* using JSP since the c tags are from the JSTL library which stands for (JSP Standard Tag Library) – Adam Jun 09 '11 at 19:03
  • @Adam: You are wrong. JSTL can be used on Facelets. See also VDL docs: http://download.oracle.com/javaee/6/javaserverfaces/2.0/docs/pdldocs/facelets/index.html Note that a subset of JSTL Core tags and full set of JSTL Functions are included. For the case you didn't realize: Facelets is the successor of JSP. Facelets doesn't understand JSP `<%@taglib%>` stuff and so on. – BalusC Jun 09 '11 at 19:07
  • @BalusC I agree that JSTL can be used on facelets, but I was clarifying that `xmlns:c="http://java.sun.com/jsp/jstl/core"` were JSP tags – Adam Jun 09 '11 at 19:09
  • @Adam: You are wrong. Only JSPX supports XML namespaces like that as well. But the OP was in essence using Facelets. JSTL are not JSP tags per se. The *real* JSP tags are ``. This is not supported on Facelets at all. Therein you use `` instead. – BalusC Jun 09 '11 at 19:10
5
<c:if test="#{myBean.myValue == 1}">

Prior to JSF2, these JSTL-like Facelets tags were only evaluated at tree creation time (ref).

The JSF2 doc for c:if doesn't mention this and I know the two Facelets versions are not compatible (that's definitely in the spec) and it isn't clear to me what "processed" means in this context. It might be worth perusing the spec to see if it describes the behaviour in more detail, check what's in your component tree at runtime and check your object state at view creation time.

McDowell
  • 107,573
  • 31
  • 204
  • 267
1

In most cases can be replaced with a rendered-Attribute, in some cases you may add some neutral extra component like .

For there is an alternative, that works more lightweight an together with the JSF Lifecycle:

<e:div>
    <h:selectBooleanCheckbox id="boolean" value="#{bean.value}" rendered="#{bean.type == 'boolean'}"/>
    ...
    <e:otherwise>
        <h:inputText id="text" value="#{bean.value}"/>
    </e:otherwise>
</e:div>

You can configure from http://www.intersult.com/wiki/page/JSF%20Ext#section-JSF+Ext-OtherwiseTag (please use page translation).

Tires
  • 1,529
  • 16
  • 27
-1

<c:if test="${not true}" > false !! </c:if>

heyhey
  • 1