Java
Java EE
| By Gregory Murray and Jennifer Ball |
XMLHttpRequest object for asynchronously exchanging the XML data between the client and the server.

XMLHttpRequest object is created and configured with a request parameter that includes the ID of the component that generated the event and any value that the user might have entered.
XMLHttpRequest object makes an asynchronous request to the web server. An object (such as a servlet or listener) receives the request, processes it, and stores any data in the request to the data store. In the case of Ajax-aware JavaServer Faces components, the object that processes the request is a
PhaseListener object. We'll cover that more later in the document.
XMLHttpRequest object receives the XML data, processes it, and updates the HTML DOM representing the page with the new data.
XMLHttpRequest
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
|
<dl:dlabel valueBinding="#{SessionBean.name} " />
|
PhaseListener class, which handles the Ajax request, interacts with the custom component, and returns an XML document that contains the updated data.
bpcatalog-ee5(2) directory.
bpcatalog-ee5-ea-0.6-installer.jar.
lib/ant/bin directory.
<bpcatalog_install>/bp-project/.
build.properties in a text editor.
javaee.home property to the path of your Application Server installation.
javaee.server.passwordfile property to the fully-qualified path to a file that contains your password. Follow the instructions in the
build.properties file.
build.properties.
<bpcatalog_install>/apps/webtier/bp-dynamic-text.
ant
.
ant deploy.
http://localhost:8080/bp-dynamic-text/
index.jsp
, a JSP page that contains the
dlabel custom JavaServer Faces component tags.
confirmation.jsp
, a JSP page that displays the updated data of the components.
DLabel
, which defines the custom component.
DLabelRenderer
, which renders the markup for the components and the
script tags that reference the JavaScript files onto the HTML page.
DLabelPhaseListener
, which processes the Ajax request, exchanges the data with the server-side object, and returns the XML data to the client.
SessionBean
, which holds the component data on the server.
script.js
, a JavaScript file that contains the functions used in this application.
styles.css
, a stylesheet.
dojo.js
, the JavaScript file that contains all the JavaScript functions supported by DOJO.
index.jsp
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
|
dl:dlabelSessionBean
The following steps explain the architecture illustrated in Figure 3:
index.jsp page contains an HTML
script tag rendered by the renderer,
DLabelRenderer. While performing the rendering of the components,
DLabelRenderer also creates a hash map with all the component IDs and values.
faces/ajax-dlabel-script.js, which is mapped to a
FacesServlet instance. This instance processes the
DLabelPhaseListener instance, which recognizes the URL and returns the
script.
js page containing the client-side JavaScript code necessary for the Ajax interactions. When the page loads, it is initialized, and the
initialize function is called. This function registers all the JavaScript events that are needed for rendering the
DLabel component tags on the page.
onclick event. The
updateOnServer JavaScript function, which is mapped to this event, creates an
XMLHttpRequest object and configures it with the URL to the
FacesServlet instance. If the user changed the Name field to Bob, the URL is
faces/ajax-dlabel-update&component-id=Name&component-value=Bob.
XMLHttpRequest
object makes a call to
FacesServlet, which updates
SessionBean.
XMLHttpRequest object makes a call to the
FacesServlet instance, passing the URL
faces/ajax-dlabel-update&component-id=Name&component-value=Bob. The
FacesServlet instance processes the
DLabelPhaseListener instance that recognizes the URL the
XMLHttpRequest object passed to it.
At this point, the
FacesServlet instance also updates the value of the
name property in
SessionBean.
ks up the component ID included in the URL and stores the new data
DLabelPhaseListener takes the component ID and value that is included in the URL, looks up the component ID and its value from a hash map, and updates it with the user's input.
DLabelPhaseListener instance generates an XML document containing the new component ID and value and returns it to the
XMLHttpRequest object.
XMLHttpRequest object updates the HTML DOM with the new data
XMLHttpRequest object calls the
XMLHttpRequest callback function. This function updates the HTML DOM based on the contents of the XML document that was returned, and the response is sent back to the client.
onmouseover events occur.
dojo.eventdojo.ioonmouseoveronclickscript.jsdojo.event.connect
bpui.dlabel = new DLabel();
|
initializescript.jsdojo.io.bindioXMLHttpRequestdojo.io.bindXMLHttpRequestXMLHttpRequest
ajax-commons.js
(as plain text)onloadinitializedlabelPlainTextgetElementsByClassRendering JavaScript Tags with a Custom RendererclassinitializeupdateItem
|
updateOnServerupdateOnServer
function updateOnServer(itemId, itemValue) {
|
updateOnServerdojo.io.bindurl: This is the URL that the
XMLHttpRequest object passes to the phase listener, which will use it to create an XML document with the component IDs and values.
method: This indicates the HTTP method to use when the function is called. In this case, we want to use POST to post data to the server.
content: This attribute contains the ID of the component the user chose to edit as well as the value the user entered into the text field.
mimetype: Indicates the MIME type of the content to be passed from the phase listener
load: Specifies the function that will be invoked after the XML data is received.
updateOnServerprocessUpdateResponseupdateOnServerprocessUpdateResponseupdateItem
function processUpdateResponse(responseXML) {
|
scriptscript.jsindex.jspconfirmation.jspdLabelencodeEndDLabelRendererscriptDLabelRendererRENDERED_SCRIPT_KEYencodeEndDLabelRendererrenderScriptOnceDLabelRendererResponseWriterscriptRENDERED_SCRIPT_KEYscript
private void renderScriptOnce(ResponseWriter writer,
|
renderScriptOnceFacesServletfaces/script
writer.startElement("script", component);
|
srcDLabelRendererResponseWriter
|
DLabelPhaseListenerDLabelRendererdivdivdivdlabelPlainTextRegistering JavaScript Events on the Componentsinitialize
writer.startElement("div", component);
|
PhaseListenerafterPhase(PhaseEvent)PhaseListenerPhaseListenerPhaseListenerscriptDLabelPhaseListenerDPhaseListener
private static final String SCRIPT_VIEW_ID = "ajax-dlabel-script.js";
|
afterPhaseDLabelPhaseListenerajax-dlabel-script.jshandleResourceReqestscript.js
public void afterPhase(PhaseEvent event) {
|
handleResourceRequestscript.jsdojo.jsresponseCompleteDLabelPhaseListenerXMLHttpRequestupdateOnServerfaces/ajax-dlabel-updateDLabelPhaseListenerXMLHttpRequestafterPhaseDLabelPhaseListenerhandleAjaxRequest
private static final String UPDATE_STATE_ID = "ajax-dlabel-update";
|
handleAjaxRequest
FacesContext context = event.getFacesContext();
|
ValueExpression valueBinding = (ValueExpression)
|
handleAjaxRequestcompIdcompValue
String message = "success";
|
updateOnServerXMLHttpUpdateResponseupdateOnServerprocessUpdateResponse