SOME SIMPLE EXAMPLES FOR
BUILT-IN AJAX IN JSF2
==========================
Let us consider the following ajaxtime.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:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html">
<h:body>
<f:view>
<h:form>
<h:commandButton id='button1' value="show time" >
<f:ajax render="label1"/>
</h:commandButton>
<h:outputText value="#{timedisplayer.time}" id="label1"/>
<br/>
<h:commandButton id='button2' value="greet" >
<f:ajax render="label2"/>
</h:commandButton>
<h:outputText value="#{timedisplayer.greeting}" id="label2"/>
</h:form>
</f:view>
</h:body>
</html>
----------------------------------------------------------------------------------------
Carefully note the <f:ajax tag.
What this page means will become clear when we see the code for
// timedisplayer.java
package mypack;
import java.util.*;
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
@ManagedBean(name="timedisplayer")
@SessionScoped
public class timedisplayer
{
int n;
public Date getTime ()
{
return (new Date());
}
public String getGreeting()
{
String greeting;
n = n+1;
if(n%2==1) { greeting="hello";}
else { greeting="welcome!";}
return greeting;
}
}
===============================
Let us now explain.
When button1 is clicked, ajax call is made to
the backing bean named 'timedisplayer'.
'render' means that label1 is to be updated by the
result of ajax call . ( label1 is bound to 'time' ).
Where is the 'time' attribute? Not necessary.
When we provide getXXX , it automatically means that.
--
Coming to button2, ajax call is made to timedisplayer.
and label2 is bound to greeting property.
The syntax is a bit confusing but the result is fine!
=====================================
Anyway, 'clicking' buttons , is NOT the ideal demo for ajax .
So, I will try to develop a demo which uses 'mouseover'.or similar action.