Monday Sep 02

JSF 2 passing parameters

PDF Print E-mail
Monday, 23 May 2011 14:44
AddThis Social Bookmark Button

Passing parameters in JSF 2

Let's take an example of a tab selection with his correspondent content rendered according to the tab selection.

We have two tabs. Clicking on the tab trigger the handleTabChange method on the tabBB backing bean.

<h:commandLink actionListener="#{tabBB.handleTabChange}" value="Tab 1">
    <f:param name="tabIndex" value="1" />
<h:commandLink actionListener="#{tabBB.handleTabChange}" value="Tab 2">
    <f:param name="tabIndex" value="2" />

In JSF 2, " f:param " tag allow you to pass a parameter to a component

We have two output panels corresponding to the tabs defined previously.

<p:outputPanel id="tab1" rendered="#{tabBB.tabIndex == 1}">
     <h3>Tab 1 Content</h3>
<p:outputPanel id="tab2" rendered="#{tabBB.tabIndex == 2}">
     <h3>Tab 2 Content</h3>

The two panels are rendered according to the current tab index selected using the expression #{tabBB.tabIndex == 1} where 1 is the tab index for the related content

Tab Backing Bean

The tab manager backing bean is a managed bean . It is defined as a session bean scope and responsible to keep and dispatch the current tab index.

public class TabBB  {
private int tabIndex = 1;   
    public int getTabIndex() {
        return tabIndex;
    public void setTabIndex(int tabIndex) {
        this.tabIndex = tabIndex;
    public boolean handleTabChange() {
        ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
        String index = externalContext.getRequestParameterMap().get("tabIndex");
        return true;

The JSF Backing bean has a a method called handleTabChange . This method is in charge to retrieve the paremeter defined using f:param and set the correponding value.

When the command link is sollicited the page is refreshed. The current tab index is updated and the correponding output panel is shown or hidden according to the current tabIndex.

That's it.

Feel free to send me your feedback or questions.

Related JSF articles

JSF 2 Param Tag

Add a child UIParameter component to the UIComponent associated with the closest parent UIComponent custom action.

Tag Information
Tag Class com.sun.faces.taglib.jsf_core.ParameterTag
TagExtraInfo Class None
Body Content JSP
Display Name None
Tags: content , method , bean , current , commandlink , value= , param , backing , outputpanel , defined

Add comment

Security code

Java Tutorial on Facebook