Asynchronous JavaScript and XML (AJAX) is a development technique that you can use to create web pages that relay information to and from a server only for the portions of pages that users edit, while still displaying the rest of the page. This technique can make web applications faster and more efficient than if an entire page had to reload after each user action. You can set up an EGL-controlled JSP file to call the JSF Handler's onPreRenderFunction and provide a limited update to the page.
If the JSF Handler needs information from the page to complete the refresh request, you can add one or more parameters to the request. The JSF Handler receives these parameters along with the request, but as before, the variables in the JSF Handler are not updated to the new values of the controls to which the variables are bound. For more information, see Updating a page with a refresh request.
The J2EELib.getQueryParameter() system function retrieves the parameters from the AJAX request, if there are any. You can also use this function to detect whether the onPreRenderFunction function has been called as the result of an AJAX refresh or submit request by checking the value of the parameter $$ajaxmode. Any value other than NULL indicates that the function has been called as the result of an AJAX refresh or submit request:
if (J2EELib.getQueryParmeter("$$ajaxmode") == null)
//Not the result of an AJAX refresh or submit request
//May be the result of an AJAX external request or
else
//The result of an AJAX request.
end
This example uses an AJAX refresh request to perform simple mathematical operations like a calculator. The page shows two input controls and a combo box with mathematical operations. The AJAX request, triggered by the combo box, passes the selected operation and the two input controls to the onPreRenderFunction of the JSF Handler, which performs the mathematical operation and updates an output control showing the answer.
The page might look like this example:
The following is the code of the JSP file:
<html>
<head>
<title>calculatorPage</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css" href="theme/stylesheet.css"
title="Style">
</head>
<f:view>
<body>
<hx:scriptCollector id="scriptCollector1"
preRender="#{calculatorPage._preRender}"
postRender="#{calculatorPage._postRender}">
<h:form id="form1" styleClass="form">
<TABLE>
<TBODY>
<tr>
<td align="left">Field1:</td>
<td style="width:5px"></td>
<td>
<h:inputText id="input1" value="#{calculatorPage.field1}"
binding="#{calculatorPage.field1_Ref}" styleClass="inputText">
</h:inputText>
</td>
</tr>
<tr>
<td align="left">Field2:</td>
<td style="width:5px"></td>
<td>
<h:inputText id="input2" value="#{calculatorPage.field2}"
binding="#{calculatorPage.field2_Ref}" styleClass="inputText">
</h:inputText>
</td>
</tr>
<tr>
<td align="left">Operation:</td>
<td style="width:5px"></td>
<td>
<h:selectOneMenu id="operationComboBox"
styleClass="selectOneMenu" value="#{calculatorPage.operation}">
<f:selectItem itemValue="add" itemLabel="add" />
<f:selectItem itemValue="subtract" itemLabel="subtract" />
<f:selectItem itemValue="multiply" itemLabel="multiply" />
<f:selectItem itemValue="divide" itemLabel="divide" />
</h:selectOneMenu>
<hx:behavior event="onblur"
target="operationComboBox" behaviorAction="get"
targetAction="updatablePanel"></hx:behavior></td>
</tr>
<tr>
<td align="left">Output:</td>
<td style="width:5px"></td>
<td>
<h:panelGroup id="updatablePanel" styleClass="panelGroup">
<h:outputText id="output" value="#{calculatorPage.output}"
binding="#{calculatorPage.output_Ref}" styleClass="outputText">
</h:outputText>
</h:panelGroup>
<hx:ajaxRefreshRequest id="ajaxRefreshRequest1"
target="updatablePanel" params="input1;input2;operationComboBox">
</hx:ajaxRefreshRequest>
</td>
</tr>
</TBODY>
</TABLE>
</h:form>
</hx:scriptCollector>
</body>
</f:view>
</html>
The following is the code of the JSF Handler that goes with this page:
package jsfhandlers;
handler calculatorPage type JSFHandler
{onPreRenderFunction = onPreRender,
view = "calculatorPage.jsp"}
field1 float;
field2 float;
operation string;
output string;
function onPreRender()
if (J2EELib.getQueryParameter("$$ajaxmode") == null)
output = "Enter values and an operation.";
else
calculateAnswer();
end
end
function calculateAnswer()
param1 float = J2EELib.getQueryParameter("input1");
param2 float = J2EELib.getQueryParameter("input2");
case (J2EELib.getQueryParameter("operationComboBox"))
when ("add")
output = param1 + param2;
when ("subtract")
output = param1 - param2;
when ("multiply")
output = param1 * param2;
when ("divide")
output = param1 / param2;
end
end
end