JSF Tutorial - JSF Param Example








We can use f:param tag to pass parameters to a component or pass request parameters.

The following code shows how to pass parameter to a UI component.

<h:outputFormat value="Hello {0}!.">        
   <f:param value="World" />
</h:outputFormat>

The following code passes request parameter along with its name.

<h:commandButton id="submit" 
   value="Show Message" action="#{userData.showResult}">
   <f:param name="username" value="JSF 2.0 User" />
</h:commandButton>




Tag Attributes

AttributeDescription
idIdentifier for a component
bindingReference to the component that can be used in a backing bean
nameAn optional name for parameter component
valueThe value stored in component

Example

The following code is from demo.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">
    <h:body>
    <h:form id="form">
      Enter your name :
      <h:inputText size="10" value="#{user.name}" />
      <br /><br />
      <h:commandButton id="submitButton" 
        value="Submit - US" action="#{user.outcome}">
        <f:param name="country" value="United States" />
      </h:commandButton>
    </h:form>
    </h:body>
</html>

The following code is from result.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"
      >
     
    <h:body>
    
    <h3>
    <h:outputFormat value="Hello,{0}. You are from {1}.">
      <f:param value="#{user.name}" />
      <f:param value="#{user.country}" />
    </h:outputFormat>
    </h3>
    
    </h:body>
    
</html>

The following code is from UserBean.java.

package com.java2s.common;


import java.util.Map;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

@ManagedBean(name="user")
@SessionScoped
public class UserBean{

  public String name;
  public String country;
  
  public String outcome(){
    
    FacesContext fc = FacesContext.getCurrentInstance();
    this.country = getCountryParam(fc);
    
    return "result";
  }

  //get value from "f:param"
  public String getCountryParam(FacesContext fc){
    
    Map<String,String> params = fc.getExternalContext().getRequestParameterMap();
    return params.get("country");
    
  }
  
  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public String getCountry() {
    return country;
  }

  public void setCountry(String country) {
    this.country = country;
  }

}


Download Param.zip





To RUN

Copy the generated WAR file from the target folder to Tomcat deployment folder and run Tomcat-Install-folder/bin/startup.bat.

After Tomcat finish starting, type the following URL in the browser address bar.

http://localhost:8080/simple-webapp/demo.xhtml