JSF panelGroup example

22 November 2013
By Gonçalo Marques
In this tutorial we will see how to use JSF panelGroup as a container to other JSF components.

Introduction

JSF panelGroup may be used as a container to other JSF components. Basically it will render a div or a span depending on the configuration we set.

The layout attribute

The layout attribute of the panelGroup will determine how the panelGroup will be rendered. panelGroup is rendered by default as a span. If the layout attribute has the value block then it will be rendered as a div.

panelGroup rendered as a div

<h:panelGroup layout="block" id="container">
  <h:outputText value="I will be rendered inside a div" />
</h:panelGroup>

The previous panelGroup will be rendered as:

panelGroup rendered as a div

<div id="container">
I will be rendered inside a div
</div>

Now if we ommit the layout attribute:

panelGroup rendered as a span

<h:panelGroup id="container">
  <h:outputText value="I will be rendered inside a span" />
</h:panelGroup>

The panelGroup will now be rendered as a span:

panelGroup rendered as a span

<span id="container">
I will be rendered inside a span
</span>

The styleClass attribute

The styleClass attribute will determine the class attribute of the generated HTML element (the CSS class used for the element):

styleClass attribute

<h:panelGroup layout="block" id="container" styleClass="someClass">
  <h:outputText value="I will be rendered inside a div" />
</h:panelGroup>

Will be rendered as:

styleClass attribute

<div id="container" class="someClass">
I will be rendered inside a div
</div>

The rendered attribute

The rendered attribute will determine if the component should be rendered by JSF:

rendered attribute

<h:panelGroup layout="block" id="container" 
    rendered="#{someBean.renderPanel}">
  <h:outputText value="I will be rendered inside a div" />
</h:panelGroup>

If the render expression evaluates to true the component is rendered by JSF, otherwise the component is not rendered.

Related Articles

Comments

About the author
Gonçalo Marques is a Software Engineer with several years of experience in software development and architecture definition. During this period his main focus was delivering software solutions in banking, telecommunications and governmental areas. He created the Bytes Lounge website with one ultimate goal: share his knowledge with the software development community. His main area of expertise is Java and open source.

GitHub profile: https://github.com/gonmarques

He is also the author of the WiFi File Browser Android application: