10

I am using JSF2 on Facelets.

I define an <ui:param> in a page:

<ui:composition template="/WEB-INF/templates/ui.xhtml"
  xmlns="http://www.w3.org/1999/xhtml"
  xmlns:f="http://java.sun.com/jsf/core"
  xmlns:h="http://java.sun.com/jsf/html"
  xmlns:ui="http://java.sun.com/jsf/facelets"
>
  <ui:param name="title" value="OnAir WebDemo" />
  ...
</ui:composition>

in the ui.xhtml template I have:

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

However, <c:if test> seems to always evaluate true, also if the <ui:param> is not specified. How can I change the code so that <c:if test> will actually evaluate false when the <ui:param> is not specified?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Dejell
  • 13,947
  • 40
  • 146
  • 229

2 Answers2

43

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! The one without /jsp works only in Facelets 1.x for JSF 1.x. 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 as you found out yourself is a good candidate, however the ui:fragment is a nicer choice since it has less overhead.

<ui:fragment rendered="#{not empty title}">
    <h1>#{title}</h1>
</ui:fragment>

Note that due to a mistake of the JSF guys in the <ui:fragment> tag definition file of the initial JSF 2.0 version, Netbeans will jerk that the tag doesn't support the rendered attribute, but this is untrue. It certainly supports it. It has been fixed in JSF 2.1 tag definition.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 1
    I've been using `h:panelGroup` just for its `rendered` attribute for _years_; I didn't know about `ui:fragment`! This substantially improves performance and reduces the amount of generated HTML in places where I conditionally render things in `ui:repeat`s over large collections. Thanks! I learn something new with every one of your answers, @BalusC. – Nick Jan 15 '13 at 18:00
  • @Nick: you're welcome. Note that `` already effectively renders nothing when there are no attributes specified which should end up in the HTML, such as `id`. But indeed, the `` saves *some* `UIComponent` encoding overhead (and is IMHO more self-documenting for the sole purpose). – BalusC Jan 15 '13 at 18:12
  • Odd, I could have sworn that I saw `` rendering an empty `` tag in the html, even when its only attribute is a `rendered` that evaluates to false, but I'm not seeing it now... – Nick Jan 15 '13 at 18:18
  • i attempted to add this to the top of my index.xhtml file, but now I receive a java.lang.NullPointerException error with really not much other info. Do you know why this would happen? – king Aug 05 '14 at 19:57
3

Well,

I used rendered instead, like this:

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

                <h1>#{title}</h1>

        </h:panelGroup>
Dejell
  • 13,947
  • 40
  • 146
  • 229
  • Haven't tried it :-) Was curious about what's the disatvantage or why is it considered a bad practice. Will update if I can try it out. – rlegendi Sep 10 '13 at 10:17
  • I think that it was voted down since !null doesn't check empty. – Dejell Sep 10 '13 at 10:23