JSF 2 + Ajax Integration Example with f:ajax Tag
January 22, 2015
This page will provide the JSF 2 and Ajax integration example with f:ajax tag. To use this tag we need to do nesting with h:inputText and h:commandButton etc. There are different attributes in f:ajax tag which is used to define Ajax execution. Some of the attributes are event, execute and render etc. Here in this page we are providing demo for f:ajax with h:inputText and h:commandButton.
f:ajax with h:inputText
To use f:ajax with h:inputText, we write it inside h:inputText. Find the code snippet.<h:inputText id="msgId" value="#{userBean.message}"> <f:ajax event="valueChange" execute="msgId" render="outputId" /> </h:inputText>
f:ajax with h:commandButton
To use f:ajax with h:commandButton, we write it inside h:commandButton. Find the code snippet.<h:commandButton value="Display" type="button"> <f:ajax event="click" execute="msgId" render="outputId" /> </h:commandButton>
f:ajax Tag Attributes
There are different attributes for f:ajax tag in JSF 2. Some of them are listed below.event: Defines the event when Ajax will run.
execute: Defines the id for which input data, Ajax will run.
render: Defines the id where the output of Ajax is displayed.
Complete Example
Now find the complete example for JSF 2 and Ajax integration. We are creating two UI, one for f:ajax with h:inputText demo and second for f:ajax with h:commandButton demo.Project Structure in Eclipse
Find the demo project structure in eclipse.
Ui for f:ajax with h:inputText
Find the XHTML.input.xhtml
<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core"> <h:head> <title>JSF 2 Ajax Integration</title> </h:head> <h:body> <h3>JSF 2 Ajax Integration</h3> <h:form id="userForm"> <h:outputText id="outputId" value="#{userBean.userMessage}" /><br/><br/> <h:inputText id="msgId" value="#{userBean.message}"> <f:ajax event="valueChange" execute="msgId" render="outputId" /> </h:inputText> </h:form> </h:body> </html>
Ui for f:ajax with h:commandButton
Find the XHTML.user.xhtml
<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core"> <h:head> <title>JSF 2 Ajax Integration</title> </h:head> <h:body> <h3>JSF 2 Ajax Integration</h3> <h:form id="userForm"> <h:outputText id="outputId" value="#{userBean.userMessage}" /><br/><br/> <h:inputText id="msgId" value="#{userBean.message}"/> <h:commandButton value="Display" type="button"> <f:ajax event="click" execute="msgId" render="outputId" /> </h:commandButton> </h:form> </h:body></html>
Managed Bean
Find the managed bean being used in demo.UserBean.java
package com.concretepage; import javax.faces.bean.ManagedBean; import javax.faces.bean.RequestScoped; @ManagedBean(name = "userBean", eager = true) @RequestScoped public class UserBean { private String message; public String getUserMessage(){ if (message == null) { return "Enter Your Message."; } return "You have Entered:"+message; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } }
Output
Access the URL http://localhost:8080/JSF2Demo-1/user.xhtmlEnter some data in text field and click on display button. We will see the output generated by Ajax for click event.

Change some data in text field . We will see the output generated by Ajax for value change event.
