views:

239

answers:

2

Hi!

I am using facelet and JSF2.

I define a parameter in a page:

<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:p="http://primefaces.prime.com.tr/ui"
 template="../templates/ui.xhtml">
 <ui:param name="title" value="OnAir WebDemo"/>

...
</ui:composition>

in the ui.xhtml I have:

<html xmlns="http://www.w3.org/1999/xhtml"
 xmlns:h="http://java.sun.com/jsf/html"
 xmlns:f="http://java.sun.com/jsf/core"
 xmlns:ui="http://java.sun.com/jsf/facelets"
 xmlns:p="http://primefaces.prime.com.tr/ui"
 xmlns:c="http://java.sun.com/jstl/core"
 >
<c:if test="#{!empty title}">
        <h1>#{title}</h1>
</c:if>
</html>

but test is always being invoked as if there is no logical condition there! How can I change the code so c:if will be invoked only if title parameter exists?

A: 

Well,

I used rendered instead, like this:

<h:panelGroup rendered="#{title!=null}">

                <h1>#{title}</h1>

        </h:panelGroup>
Odelya
A: 

The XML namespace is invalid. It should be

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

Yes, astonishingly with the jsp part in the URI! If you have checked the generated HTML output in the webbrowser, you should have noticed as well that the <c:if> is left unparsed in the HTML output.


That said, you should prefer JSF components over JSTL tags, unless technically impossible (i.e. when you actually want to control the building of the view, not rendering of the view). The h:panelGroup is a good candidate, the f:verbatim is however a nicer choice since it has less overhead.

<f:verbatim rendered="#{!empty title}">
    <h1>#{title}</h1>
</f:verbatim>
BalusC