Magazine
 
JSF- Java Server Faces
 
Using Ajax in Apache MyFaces

Ajax, or AJAX, is a web development technique used
for creating interactive web applications. The intent is to
make web pages feel more responsive by exchanging
small amounts of data with the server behind the scenes,
so that the entire web page does not have to be reloaded
each time the user requests a change. This is intended to increase the web page’s interactivity, speed, functionality, and usability.

Ajax is a cross-platform technology compatible with different operating systems, computer architectures, and web browsers as it is based on open standards such as JavaScript and XML, together with open source implementations of other required technologies.

Ajax has a big name in the field of web development. One of the biggest advantages of Ajax is that it really improves web application user experience. Ajax is highly compatible with various technologies like HTML, JSP, JSF (Java Server Faces) etc. Traditionally, when the user requests for the page, the entire web page is rendered while in the case of Ajax only a portion of page is rendered.

What is Ajax?

Ajax is a new approach for creating interactive web applications. It is an acronym for “Asynchronous JavaScript and XML”. Ajax is a bundle of commonly used technologies:

1 HTML (or XHTML) and CSS
2 Document Object Model (DOM)
3 XML
4 JavaScript, XMLHttpRequest Object

Hence, Ajax is not a new technology. An Ajax application
looks as if it resideson the user’s machine. Data is asynchronously fetched and browser is updated with the gathered information rather than refreshing the entire page i.e. partial refreshing of a page.

Two technologies that will enhance Rich Internet Applications:

AJAX is a perfect match for the JSF presentation tier
because of its rich component model. Simply use JSF
AJAX components to add AJAX functionality to a JSF
application.
JavaServer(tm) Faces (JSF) is an established web application framework standard (JSR127) that accomplishes the MVC paradigm. JSF is comparable to the well-known Struts framework but has features and concepts that are beyond those of Struts. MyFaces Core is a 100% compliant implementation of this standard - it is compliant to the standard yet
 

offers additional value by providing separate extended component sets and many special features which make JSF development easier.

MyFaces has several subprojects, where MyFaces API and MyFaces Impl together comprise the core implementation. Tomahawk (MyFacesComponents), Tobago (Tobago) and Trinidad (Trinidad) are sets of extended components. These components offers you more functionality and flexibility than using the standard components of the core implementation. Integrating AJAX in Java Server Faces (MyFaces) :

Apache MyFaces has a sub-project named “Sandbox” which provides a set of components. Sandbox is a new addition to the Tomahawk project. This helps us to easily integrate Ajax enabled components in MyFaces. To let the sandbox components work, we have to add “tomahawk-sandbox-1.1.6- SNAPSHOT .jar” file in lib folder of your application along with complete set of jar files for JSF (MyFaces) environment to run application.

Download “tomahawk-sandbox-1.1.6-SNAPSHOT.jar” file:
“tomahawk-sandbox-1.1.6-SNAPSHOT.jar” file can be downloaded from http://people.apache.org/builds/myfaces/nightly/ tomahawk-sandbox-1.1.6-SNAPSHOT-bin.zip address. Just unzip the file, pick the jar file from the lib folder and copy it to the lib folder of your application. Restart the server and start using sandbox components in your application.

Example of using Ajax in MyFaces through Sandbox component

Auto Complete example:


In this example, user can only enter the name of a state (from U.S.). As soon as the user enters any string matching the states name, a drop down list comes in the picture. That helps to select the state from there. Sandbox provides a very nice component InputSuggestAjax that is a very good example of using Ajax in MyFaces. We have nothing to do just simply use the tag for the component in the view page and create backing bean to provide functionality for selecting the list of appropriate states matching the string entered by the user in the page. Just follow these steps:

1 Create View page (JSP file)
2 Create JSF managed bean

Creating JSP file:

<%@ taglib uri=”http://java.sun.com/jsf/html” prefix=”h”%>
<%@ taglib uri=”http://java.sun.com/jsf/core” prefix=”f”%>
<%@ taglib uri=”http://myfaces.apache.org/tomahawk”
prefix=”t”%>
<%@ taglib uri=”http://myfaces.apache.org/sandbox”
prefix=”s”%>
<f:view>

August 2007 | Java Jazz Up |15
 
previous
index
next
 
Pages: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
31, 32, 33, 34, 35, 36, 37, 38, 39, 40         Download PDF