JSF 2 CommandLink and CommandButton with AJAX Example

Asynchronous JavaScript XML (Ajax) is considered for a long time as a luxury, either users or developers, but today Ajax is essential for building compelling and competitive applications. JSF 2.0 has built-in Ajax support, with a standard JavaScript library.

JSF Ajax requests partially process components on the server, and partially render components on the client when the request returns. Using of Ajax has multiple forms, this post will cover Ajaxifying of CommandLink  and CommandButton jsf component, whereas the others will be covered at other posts.

Also Read:

1. The View


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

<html xmlns="http://www.w3.org/1999/xhtml"
	<h:outputScript library="javax.faces" name="jsf.js" target="head" />
	<f:view afterPhase="#{indexBean.phaseListener}"
		<h1>JavaBeat JSF 2.2 Examples</h1>
		<h2>JSF2 Ajaxify UI CommandLink &amp; CommandButton Example</h2>
			<h3>This Ajax is Triggered On Action Click</h3>
			<h:inputText id="input" value="#{indexBean.message}"/>
			#{' '}
			<h:commandButton value="Display" action="#{indexBean.displayText}">
				<f:ajax event="click" execute="input" render="output"></f:ajax>
			#{' '}
			<h:commandLink value="Display" action="#{indexBean.displayText}">
				<f:ajax event="click" execute="input" render="output"></f:ajax>
			#{' '}
			<h:outputText id="output" value="#{indexBean.message}"></h:outputText>

2. Managed Bean

package net.javabeat.jsf;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.PhaseEvent;

public class IndexBean {
	private String message;

	public String getMessage() {
		return message;

	public void setMessage(String message) {
		this.message = message;

	public String displayText(){
		return "";

	public void phaseListener(PhaseEvent e){

3. The Deployment Descriptor (web.xml)


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	id="WebApp_ID" version="2.5" metadata-complete="true">
		<description>State saving method: 'client' or 'server'
						(=default). See JSF Specification 2.5.2
		<servlet-name>Faces Servlet</servlet-name>
		<servlet-name>Faces Servlet</servlet-name>
		<servlet-name>Faces Servlet</servlet-name>

4. JSF 2 Ajaxify CommandLink & CommandButton Demo

The below snapshot shows you the Ajaxifyig implementation for commandLink & commandButton.

JSF 2 Ajaxifying Action Source

  • Ajaxifying of Action Sources (CommandLink and CommandButton) requires to use f:ajax.
  • The identification of components that being sent or rendered partially has been done through execute and render respectively.
  • Only those components that are mentioned in the f:ajax render attribute gets rendered while the jsf framework has drawn the component view.
  • Only those components that are mentioned in the f:ajax execute attribute gets executed while the submission being processed.
  • The f:ajax event attribute determines the even on which the ajax request has submitted.

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Pin It on Pinterest

Share This

Share this post with your friends!

Share This

Share this post with your friends!