Passing Parameter From JSF 2 Page To Backing Bean

Suppose you have two jsf pages, one for inserting record and other for updating and you want to redirect the both pages to confirm page before actually processing the business logic. In confirm page you have to decide two alternative action based on which page the confirm page redirected from.

In this example I will show you how to do this by using h:commandButton.
This can be done by using the following two ways-

By using ManagedProperty

<h:commandButton action="#{userBean.action}">
       <f:param name="someparam" value="insert" />
</h:commandButton>

With a @ManagedProperty, which sets the parameter as managed bean property.
@ManagedProperty("#{param.someparam}")
private String someparam;

By using f:setPropertyActionListener

<h:commandButton action="#{userBean.action}">
       <f:setPropertyActionListener target="#{userBean.someparam}" value="update" />
</h:commandButton>

Full Example:
index.xhtml
Here I am using two command button in one page but you can use different pages.

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:ui="http://java.sun.com/jsf/facelets">
    <h:head>
        <title>Ajax Example</title>
        <style>
            thead{
                background-color: darkgreen;
                color: white;
            }
            td{
                font-weight: bold;
            }
        </style>
    </h:head>
    <h:body>
            <h:form>
                <h:panelGrid id="panel4" columns="1" border="1" 
                             cellpadding="5" cellspacing="1">
                    <f:facet name="header">
                        <h:outputText value="JSF2 Action parameter"/>
                    </f:facet>
                    <h:commandButton action="#{userBean.action}" value="Pass Value using ManagedProperty">
                        <f:param name="someparam" value="insert" />
                    </h:commandButton>
                    <h:commandButton action="#{userBean.action}" value="Pass Value using PropertyActionListener">
                        <f:setPropertyActionListener target="#{userBean.someparam}" value="update" />
                    </h:commandButton>
                </h:panelGrid>
            </h:form>
    </h:body>
</html>

UserBean.java

package com.example.javaknowledge;

import com.example.javaknowledge.dao.Database;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.model.SelectItem;

@ManagedBean
public class UserBean {

    @ManagedProperty("#{param.someparam}")
    private String someparam;

    public String getSomeparam() {
        return someparam;
    }

    public void setSomeparam(String someparam) {
        this.someparam = someparam;
    }

    public String action() {
        return ("confirm");//will redirect to confirm.xhtml page
       
    }

    public void dispaction() {
        System.out.println(someparam);
       //set your logic based on paramvalue
    }
}

confirm.xhtml

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"   
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:ui="http://java.sun.com/jsf/facelets">
    <h:body>
        <h:form>
            <h:panelGrid id="panel1" columns="1" border="1" 
                         cellpadding="5" cellspacing="1">
                <f:facet name="header">
                    <h:outputText value="JSF2 Action parameter"/>
                </f:facet>
                <h:commandButton action="#{userBean.dispaction}" value="Confirm">
                    <f:param name="someparam" value="#{userBean.someparam}" />
                </h:commandButton>
            </h:panelGrid>
        </h:form>
    </h:body>
</html>
 

admin

Hello! I am Md. Abdul Bari; founder and admin of javaknowledge; currently providing training as a J2EE faculty in The Computers Ltd. Dhaka, Bangladesh under an IT Scholarship project provided by IDB-BISEW. I am a self learner and passionate about training and writing. I am always trying my best to share my knowledge through my blog.

More Posts - Website

Follow Me:
TwitterFacebookGoogle Plus

13,861` Total Views

Leave a Reply

Your email address will not be published. Required fields are marked *


1 + eight =