2
votes

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?

2
you could specify if jsf 1.2 or jsf 2.0 - Cosmin Cosmin
The <h:panelGroup> does not insert a span at all when you don't specify any attributes which needs to be generated to HTML like id, styleClass, etc. - BalusC

2 Answers

5
votes

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>
3
votes

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