JSF f:param tag is used to pass parameters to JSF UI Component.
Note:
1. If f:param component is used with h:outputFormat then it specifies the placeholder.
2. If f:param component is used with other component then it specifies the request parameter.
Attributes of f:param tag.
Attribute | Description |
---|---|
id | Identifier for a component |
binding | Reference to the component that can be used in a backing bean |
name | An optional name for parameter component |
value | The value stored in component |
Example:
Test.java
import java.util.Map; import javax.faces.bean.ManagedBean; import javax.faces.bean.SessionScoped; import javax.faces.context.FacesContext; /** * Managed bean. * @author w3schools */ @ManagedBean(name="test") @SessionScoped public class Test { private String userName; public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String displayParam() { FacesContext fc = FacesContext.getCurrentInstance(); Map<String,String> params = fc.getExternalContext().getRequestParameterMap(); userName = params.get("userName"); return "success"; } } |
test.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:head> <title>JSF param example.</title> </h:head> <h:body> <h3>JSF param example.</h3> <h:form> <h:outputFormat value="Hello {0}."> <f:param value="World" /> </h:outputFormat> <br/><br/> <h:commandButton id="submit" value="Display Param" action="#{test.displayParam}"> <f:param name="userName" value="Jai" /> </h:commandButton> </h:form> </h:body> </html> |
welcome.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:head> <title>JSF param example.</title> </h:head> <h:body> <h3>#{test.userName}</h3> </h:body> </html> |
faces-config.xml
<?xml version="1.0" encoding="windows-1252"?> <faces-config version="2.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xi="http://www.w3.org/2001/XInclude" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"> <navigation-rule> <from-view-id>test.xhtml</from-view-id> <navigation-case> <from-outcome>success</from-outcome> <to-view-id>welcome.xhtml</to-view-id> </navigation-case> </navigation-rule> </faces-config> |
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <servlet> <servlet-name>faces</servlet-name> <servlet-class> javax.faces.webapp.FacesServlet </servlet-class> </servlet> <servlet-mapping> <servlet-name>faces</servlet-name> <url-pattern>/faces/*</url-pattern> </servlet-mapping> </web-app> |
URL:
http://localhost:7001/JSFExample22/faces/test.xhtml
Output:
Click on Display Param button.
Download this example.