Saturday, 1 March 2014

JSF 2.0 with Ajax - Simple Example

In this example,we will see how we can use ajax with JSF 2.0.
To understand the concept in a simple manner,lets take our previous example where we were submitting the form on click of button.Here we will make ajax call instead of calling the whole page.

firstly create a simple indexpage.xhtml page.
1.indexpage.xhtml
  

     

JSF 2.0 with Ajax Example

The code of HelloBean.java will be like this.
HelloBean.java
package org.mani.beans;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
/**
 *
 * @author mani
 */
@ManagedBean
@SessionScoped
public class HelloBean {
    private String name;
    
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    
    public String getMakeAjaxCall(){
   
    if(name ==null){
  return "";
    }else{
  return  "Welcome " + name;
    }
 }
    
}


When you click on "Click Me",an ajax call is made and message is refreshed without refreshing whole page.
If all the things go well,the output will be like this..