2

I have something like this:

<ul>        
<li>User</li>
<li>Admin</li>
..
</ul>

and want to render the list-item 'Admin' only if the actual User is in Role 'Admin'.

I know, I can do that like this:

<ul>        
<li>User</li>
<h:panelGroup rendered="#{request.isUserInRole('admin')}">
<li>Admin</li>
</h:panelGroup>
..
</ul>

However the markup inserts a "span"-element because of h:panelGroup.

Is there a JSF-Component with a 'rendered'-property which does not insert any html?

Unfortunately, Facelets ui:remove/ui:include has not a 'rendered' property:

<ul>        
<li>User</li>
<ui:remove rendered="#{request.isUserInRole('admin')}">
<li>Admin</li>
</ui:remove>
..
</ul>

How should I solve such a scenario?

nimo23
  • 5,170
  • 10
  • 46
  • 75

2 Answers2

5

What you mean can be achieved by using ui:fragment.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
                xmlns:ui="http://java.sun.com/jsf/facelets">

    <ui:fragment rendered="#{action.isUserLoggedIn()}">
        <!-- this will only be shown to logged in users -->
    </ui:fragment>
</ui:composition>
Dario Pedol
  • 2,070
  • 14
  • 24
  • Thanks - It works!! Where did you see, that ui:fragment has a rendered-property. According to http://docs.oracle.com/cd/E17802_01/j2ee/javaee/javaserverfaces/2.0/docs/pdldocs/facelets/index.html there is no rendered-property for this component (And my IntelliSense does not show this property, either). However, it works. – nimo23 May 04 '12 at 07:55
  • I am wondering, if I am better to use JSTL c:if for such cases. As c:if only evaluates once the view is rendered, which is more efficient. Am I right? – nimo23 May 04 '12 at 07:56
  • Okay I found a similar case http://stackoverflow.com/questions/6015609/jsf-uifragment-rendered-performance which claims that c:if would be better for one-rendering-cases. As roles does not change during runtime, I am better to use c:if. – nimo23 May 04 '12 at 08:00
  • 1
    c:if actually isn't JSF but JSTL and is not included in the component tree at all. It sometimes leads to errors that are very hard to find and/or comprehend. I strongly suggest you don't use it. You probably can't even measure the performance difference in most applications. – Dario Pedol May 04 '12 at 08:09
  • Okay, thanks! I will follow your advise, I will use ui:fragment instead of h:panelGroup. – nimo23 May 04 '12 at 08:33
3

Is there a JSF-Component with a 'rendered'-property which does not insert any html?

You already have your answer, just for the record the panelGroup component do not render any html providing that you don't include the id and style attributes. For example

<ul>
    <li>Hello</li>
  <h:panelGroup rendered="#{expression}">
     <li>world</li>
  </h:panelGroup>
</ul>

will be rendered as

<ul>
 <li>Hello</li>
 <li>world</li>
</ul>

if expression is true

and

<ul>
 <li>Hello</li>
</ul>

if expression if false

dimcookies
  • 1,930
  • 7
  • 31
  • 37
  • h:panelGroup sometimes doesn't output span or div ? – Cosmin Cosmin May 04 '12 at 08:27
  • Yes, you are right. I works, too. I red the API for h:panelGroup which states that if layout-attribute is missing it will be rendered as a span-element. Maybe I did misunderstand that. But after looking at the rendered markup, there is (thankfully) no span/div element, when not declaring layout or id for h:panelgroup. – nimo23 May 04 '12 at 08:30