Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Jsf + Jstl + Facelets problem

balteoDec 7 2010 — edited Dec 10 2010
Hello,
I have the following xhtml facelets pages:

menu.xhtml
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
                xmlns:ui="http://java.sun.com/jsf/facelets"
                xmlns:h="http://java.sun.com/jsf/html"
                xmlns:f="http://java.sun.com/jsf/core"
                xmlns:a4j="http://richfaces.org/a4j"
                xmlns:pretty="http://ocpsoft.com/prettyfaces"
                xmlns:c="http://java.sun.com/jsp/jstl/core">
    <ul>
        <!-- accueil -->
        <c:choose>
            <c:when test="#{page==accueil}">
                <li id="active"><a href="#" id="current"><h:outputText value="#{msg['menu.accueil']}" /></a></li>
            </c:when>
            <c:otherwise>
                <li><pretty:link id="accueil" mappingId="rootAction"><f:param value="#{view.locale.language}"/><h:outputText value="#{msg['menu.accueil']}" /></pretty:link></li>
            </c:otherwise>
        </c:choose>
        <!-- galerie -->
        <c:choose>
            <c:when test="#{page==galerie}">
                <li id="active"><a href="#" id="current"><h:outputText value="#{msg['menu.galerie']}" /></a></li>
            </c:when>
            <c:otherwise>
                <li><pretty:link id="galerie" mappingId="galerieAction"><f:param value="#{view.locale.language}"/><h:outputText value="#{msg['menu.galerie']}" /></pretty:link></li>
            </c:otherwise>
        </c:choose>
        <!-- biographie -->
        <c:choose>
            <c:when test="#{page==biographie}">
                <li id="active"><a id="current" href="#"><h:outputText value="#{msg['menu.biographie']}" /></a></li>
            </c:when>
            <c:otherwise>
                <li><pretty:link id="biographie" mappingId="biographieAction"><f:param value="#{view.locale.language}"/><h:outputText value="#{msg['menu.biographie']}" /></pretty:link></li>
            </c:otherwise>
        </c:choose>
        <!-- commandes personnalisées -->
        <c:choose>
            <c:when test="#{page==commandesPersonnalisees}">
                <li id="active"><a id="current" href="#"><h:outputText value="#{msg['menu.commandes-personnalisees']}" /></a></li>
            </c:when>
            <c:otherwise>
                <li><pretty:link id="commandes-personnalisees" mappingId="commandesPersonnaliseesAction"><f:param value="#{view.locale.language}"/><h:outputText value="#{msg['menu.commandes-personnalisees']}" /></pretty:link></li>
            </c:otherwise>
        </c:choose>
        <!-- contact -->
        <c:choose>
            <c:when test="#{page==contact}">
                <li id="active"><a href="#" id="current"><h:outputText value="#{msg['menu.contact']}" /></a></li>
            </c:when>
            <c:otherwise>
                <li><pretty:link id="contact" mappingId="contactAction"><f:param value="#{view.locale.language}"/><h:outputText value="#{msg['menu.contact']}" /></pretty:link></li>
            </c:otherwise>
        </c:choose>
        <!-- liens -->
        <c:choose>
            <c:when test="#{page==liens}">
                <li id="active"><a href="#" id="current"><h:outputText value="#{msg['menu.liens']}" /></a></li>
            </c:when>
            <c:otherwise>
                <li><pretty:link id="liens" mappingId="liensAction"><f:param value="#{view.locale.language}"/><h:outputText value="#{msg['menu.liens']}" /></pretty:link></li>
            </c:otherwise>
        </c:choose>
    </ul>
</ui:composition>
and in my home page:
 <ui:include src="WEB-INF/include/menu.xhtml">
       <ui:param name="page" value="accueil"/>
 </ui:include>
The unexpected result is as follows: both the contents of <c:when... and <c:otherwise are displayed!!!

Can anyone please help?

Thanks in advance,

J.

Edited by: balteo on Dec 8, 2010 8:49 AM

Comments

gimbal2
I didn't want to reply initially because I have little to say that could help you solve your problem, but since nobody is replying I'll go ahead and do it anyway.


It is generally discouraged to use JSTL together with JSF; results tend to be unpredictable. The choose/when/otherwise of JSTL can easily be replaced in plain old JSF with the usage of the rendered attribute for example.
balteo
Thanks a lot for your reply Gimbal!

Here is what I want to achieve:
"if the condition is met include the <li without the link."
"else include the <li with the link" but most importantly I don't want any other html content (such as a <div) in my <ul and <li.

Bearing the above in mind:
1. How do I deal with html and jsf content using plain old jsf (see my first post i.e. <li and <pretty:link components)?
2. Is there a way for my dynamic menu presentation logic to be exexuted at facelet compilation time?

J.
gimbal2
generally, without using something like richfaces and its a4j:outputPanel tag, I tend to create dynamic parts in a JSF page by using the ui:fragment tag.
<ui:fragment rendered="#{user.loggedIn}">
Hey, you are logged in!
</ui:fragment>
<ui:fragment rendered="#{not user.loggedIn}">
Hey, you are not logged in!
</ui:fragment>
Does that help you?

btw:
2. Is there a way for my dynamic menu presentation logic to be exexuted at facelet compilation time?
As far as I know, there is no such thing. A facelets page may be cached, but it is never 'compiled'. Or am I simply understanding you badly and you mean at the time the JSF component tree is built?
balteo
Hello Gimbal,
The code fragment you included is interesting. I actually considered using this very tag/attribute combination but unfortunately it seems the "rendered" attribute is no longer available with the ui:fragment tag (see google).
The simple solution is to use h:panelGroup with only the rendered attribute. Thus no <span or <div tag are placed by jsf.
Thanks a lot to you!
J.
gimbal2
Really? We're talking about JSF 2.0 here I presume; I was under the impression that it was supposed to be backwards compatible with JSF 1.2. Dropping the rendered attribute from ui:fragment certainly does not help there :s

Did you try it? Perhaps the documentation is just flawed.
balteo
Netbeans won't accept it suggesting it is not in the jar. :-(
gimbal2
Hur. This smells like an oversight to me, but who am I to question what is design and what is flaw? I'm not using JSF 2.0 yet, so I'm not intimately familiar yet with everything that has changed in it :/
EJP
Hmm. ui:fragment was introduced in JSF 2.0 according to Ed Burns' book.
gimbal2
EJP wrote:
Hmm. ui:fragment was introduced in JSF 2.0 according to Ed Burns' book.
I'm living in the twilight zone.

I have only one explanation: it might be because I use the facelets 1.15.b1 version (to fix null-pointer issues with JBoss 5.1). Its weird because I do actually use ui:fragment in a plain old JSF 1.2.12 project (but with facelets 1.15.b1) and I use succesfully the rendered attribute on it :s

Given the information received in this thread, I would completely disregard what I said about this and I'm going to consider replacing the ui:fragment stuff with a4j:outputPanel in my app. Thanks for the insights!
1 - 9
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Jan 7 2011
Added on Dec 7 2010
9 comments
232 views