Académique Documents
Professionnel Documents
Culture Documents
This learning module covers the use of AJAX technology to enable better response
and performance in your EGL/JSF Web Pages.
Units:
Course Setup
Web/JSF Overview
JSF Properties Deep Dive
Essential JSF Components
Additional JSF Components
JSF dataTables
Page Flow and State Management
AJAX Controls and JavaScript
JSF Component Tree and Dynamic Property Setting
Web-Site Design and Template Pages
Appendices
Internationalization
Page Design Best Practices
Additional Use Cases
HTTP Request
HTTP Request
Synchronous Data Transfer
Asynchronous Data Transfer
HTML +CSS + data
Application Application
Server Server
EGL Application EGL Application DB
Process data Process data
DB Access database Access database
Call legacy systems Call legacy systems
Last update: 12/04/2007 5
AJAX Page Requests
There are three major categories of AJAX page update processing:
1. Refresh Request
With Refresh Request you define individual parameters to AJAX, and only the
values in the specified parameters are sent to your JSFHandler (data
transmission is minimized). Data can be returned from your JSFHandler to an
area of the page defined under AJAX control (typically inside a JSF PanelGroup
Box enabled for AJAX update).
2. Submit Refresh
An AJAX Submit Refresh allows you to define areas of your page under AJAX
control (like Refresh Request) but instead of sending only individual parameters
to your JSFHandler, the entire form is submitted – so your JSFHandler can
reference any user value on any field in the form. See Notes for additional
benefits of Submit Refresh.
3. External Request
Allows a parent page to receive external values from a child page – or, can be
used with multiple pages on the same page (example later in this section).
2. Develop the EGL JSFHandler and business logic (been there/done that too)
As you did in the last section , you will call a j2eelib.function to return an AJAX request
parameter – specifically: j2eelib.getQueryParameter(…)
3. Design the Page with JSF components bound to EGL data (been there/done that). However, you will
use one or more JSF Panel Group controls to organize the AJAX requested data:
JSF Panel Group – if only JSF components are being added…or…
JSF JSP Panel embedded inside the JSF Panel Group – if JSP + HTML tags are to be
embedded or used in the design (example later in this section)
4. Create a JavaScript event for a JSF component, that invokes AJAX behavior for a target JSF Panel
A Group component:
J
Note that this essentially creates the JavaScript call to the AJAX engine that creates the HTTP
A
X request to your JSFHandler
S 5. Enable the AJAX container for update, and create a Refresh Request on the JSF Panel Group,
T adding parameters for data to be sent from the AJAX engine to your JSFHandler
E
P
S
Perhaps a bit more detail is in order before we dive in with a workshop.
1. Use of preRender()
preRender() is called every time a page is requested (see notes on the difference between preRender() and
onConstruction(). AJAX logic will go in preRender().
2. J2eelib.getQueryParameter(“…”)
This EGL Built-in function allows you to access the value sent in via the AJAX parameter request
1.
2.
3.
3.
DB
StateAbbrev From the *Quick Edit view, select the onchange event - specify:
Select StateAbbrev.
Use pre-defined behavior
Action: Invoke Ajax behavior on the specified tag
Target: group1 (this directs AJAX to render data in the Panel Group Box)
1.
2.
Last update: 12/04/2007 16
5. Create an AJAX Refresh Request on the Panel Group – 2 of 2
From the hx:ajaxRefreshRequest property:
Click: Add Parameter
(Under Parameter values sent from the browser)
Open the Combo-Box and select: textCustState1
Again click: Add Parameter
Open the Combo-Box again, and select: menuStates1
Note – These will be the parameters AJAX sends to your JSFHandler in each request. You
will retrieve each separately using a j2eelib.getQueryParameter(…) function
That’s all there is to it! You’re ready to test your first AJAX page.
Last update: 12/04/2007 17
Run the Page
Type an upper-case state abbreviation: NY …or… NJ …or… CA …or… CT
Click Search
Note the speed with which the data is returned, and note that the top part of the page doesn’t “blink” (more
specifically, the entire page does not get re-loaded) when the data is returned
When you’re satisfied with this example: Edit the JSFHandler.
Comment out: // custState = j2eelib.getQueryParameter("textCustState1");
Un-comment:
Un-comment: custState = j2eelib.getQueryParameter("menuStates1");
Here’s an
extension to the
page we’ve been
working on –
passing a
parameter as an
AJAX request
from a clicked-row
inside a
DataTable.
This particular
Use Case is a bit
“oblique” but this
page pattern has
lots of practical
applications in
real-life scenarios
1.
2.
Comment out
3.
Ctrl/S
Last update: 12/04/2007 20
3. Design the Page – Add a new Column and the dataTable – 1 of 2
You will add the Customers array to the page as a read-only dataTable. To do this you will need to add a
column to the existing HTML table, then place the array in the table. As of this release – dragging and
dropping an array in an HTML table is a two-step procedure…
2.
1.
Customers array
dataTable
2. (From the Palette) Drag an Input – Hidden component into the State column of the dataTable (see
screen capture). From Properties,
Properties ensure that its
Id is StateH
Value is Browse and from Page Data select Customers.State
3. Select the control (the output field, not the hidden one you just added) and from the Palette Double-Click Link
to add a Faces Link to the State field. Do not fill in the URL**
Notes
$$AJAXROW$$ - JSF/AJAX statement prefix, i.e. the “current/clicked row” of a dataTable
form1:tableEx2: - The fully-qualified HTML name of the dataTable**
StateH - A hidden variable.
- StateH is what the getQueryParameter references:
Note also that colons : separate the three-part field name form1:tableEx2:StateH
Let’s begin:
Create a new page, named:
ajaxSubmitRefresh.jsp
- For now, just change the
page heading text as
shown in the screen
capture
Last update: 12/04/2007 28
2. JSFHandler Code
Copy the code in the Notes section of
the slide, and replace the boilerplate 1.
JSFHandler code.
Note the following:
1. There are two dynamic arrays for
the ComboBoxes (note their
properties), and a dynamic array
of orders
2.A case statement in
preRender() tests a hidden
variable on the form – which
determines the ComboBox that
was selected – and calls one of
two functions that will return 2.
dynamic array rows from different
tables.
3. In the CustomersCB array we
are inserting a row 1 – if
customers are found – this will
remind users to select a
customer.
StatesCB
CustomersCB
Inside a JSP Panel Group Box
Inside a Group Panel Group box
ordersArray
Inside a Group Panel Group box
hidden1
Last update: 12/04/2007 31
5. Create the JavaScript Event to Invoke the AJAX Engine
Select the StateAbbrev ComboBox.
From the *Quick Edit view, select the onchange event - specify:
- Use pre-defined behavior
- Action: Invoke Ajax behavior on the specified tag
- Target: group1 (this directs AJAX to render data in the Panel Group Box)
With the ComboBox still selected, add the following line of JavaScript in the Quick Edit view
document.getElementById("form1:hidden1").value = 'combobox1';
From the *Quick Edit view, select the onchange event - specify:
- Use pre-defined behavior
- Action: Invoke Ajax behavior on the specified tag
- Target: group2 (this directs AJAX to render data in the Panel Group Box)
With the ComboBox still selected, add the following line of JavaScript in the Quick Edit view
document.getElementById(“form1:hidden1”).value = ‘combobox2’;
New York
Filibuster
Lundquist
Note that we have not done much with error-handling. For example, what would be
appropriate to show users if there were no customers for a selected state? An
optional workshop coming up will show you some examples for this.
Last update: 12/04/2007 36
EGL/JSF Steps to Implementing AJAX External Refresh Functionality
The AJAX External Refresh allows you to use the AJAX engine to load additional
(child) pages inside PanelGroup boxes in an existing (parent) page. The steps to
do this are shown below – and would be used to create new pages from scratch.
We’ll be re-purposing existing pages (so some steps will be consolidated)
2. Develop the EGL JSFHandler and business logic (been there/done that too)
3. Design the Page with JSF components bound to EGL data (Yep - been there/done that).
4. Create a JavaScript event for a JSF component, that invokes AJAX behavior for a target
JSF panelGroup component (been there/done that in the last workshop)
5. Enable AJAX for update, and create an External Request on the JSF Panel Group
Wow – less to keep track of than before. Maybe we’ve “caught a break” with this one!
Then, Drag and drop a JSF PanelGroup Box (Type: Group) onto the page
PanelGroup box
From the
hx:ajaxExternalRequest $$AJAXROW$$form1:tableEx1:cid
sub-property, add a new
parameter – exactly as
shown here
You may have noticed that the JSF pager controls invoke your EGL JSFHandler through a
Form/Submit. This may be okay for some functionality, but you also might want: 1. A better
(AJAX-matized) user experience, and 2. more programmatic control over your paging.
Let’s try creating our own AJAX-controls paging for a dataTable, and see if you think this approach
has merit. Here’s what we’re going to build (at run-time). The AJAX techniques we’ll use have
already been covered in this Unit, so this should be fairly simple and straightforward to put
together.
Customers
JSF array
Panel as a
Group dataTable
group1
JSF
Panel
Group Functions as submit buttons – with images
group2
Specify:
Allow Ajax updates
Submit
To work-around this, we need to trigger the onLoad javascript function (on slide 257 notes section) method
every time a payload is received from an AJAX request and the request has completed. The following steps
assume you have added the JavaScript for mouse-over row/color (this code has been added to this slides:
***Notes for your convenience) to a .JSP page with a dataTable under AJAX control.
Steps:
Remove the onload= ... from the body tag.
Locate the h:panelGroup for the table tableEx1
Click on the Ajax tab under h:panelGroup
Click on “edit the Ajax request properties” icon on the bottom of the page.
This will bring up methods that can be used during the lifecycle of an Ajax request.
Click on “oncomplete” and type: onLoad(‘form1:tableEx1’);
Run the page on the server – and test the combined Mouse-over color + AJAX
You may also wish to provide dynamic AJAX-enabled server-side database access in your
pages, similar to type-ahead. Here’s a working page that shows how to do the following:
Users type in (case-sensitive) search arguments (one keystroke at a time), and the
page’s server-side EGL functionality does a database lookup, filtering rows dynamically.
Note also the mouse-over alternate colors. And by clicking a row in the dataTable, you fill
in the Customer’s Address directions (again, using AJAX)
Notes:
The AJAX functionality is fired
off by onKeyUp from the
first input field
The 2nd Submit Button will not
render (it uses a hidden .css tag)
In some large and complex pages, where you have multiple tab panels, each of which
contains one or more dataTables, you may wish improve the overall page’s loading speed
and performance, by using AJAX and hiding/rendering selected panel components. This
workshop will show you how to create a tab page, with panels that:
Hide and render dataTables programmatically (events fired when users click a panel tab)
Use AJAX to improve dataTable and page U.I. performance
document.getElementById('form1:hidden1').value = "tab3";
Tab3: document.getElementById('form1:button2').click();
See ***Notes
Last update: 12/04/2007 61
Optional Workshop – Combining AJAX With Tab Pages – 5 of 6
Finished Page:
• Search for customers, with LastNames > :hostVar
The AJAX-specific properties of this Workshop are similar to previous ones: (use of panelGroup, etc. use of a
hidden-input field in the dataTable column, use of a clicked Link, etc.). However, for this clicked link, note:
1. The onclick event contains JavaScript statements to return the address of the tab control in the browser and manipulate
individual panel actions (hiding/showing/and switching).
2. There are multiple AJAX Action/Targets – which is how data is loaded into two separate AJAX-controlled zones in this
page with one user-event. Note also the that panelGroup JSF IDs are fully qualified: form1:groupx
Run the page, and watch the JavaScript and AJAX operations and events. Note that the JavaScript will open
tabs, before the server-side AJAX data operations, etc.
In addition to the above, there are two incredibly well-written technical articles in DeveloperWorks that
should be considered “required reading” before making the decision for/against AJAX**
http://www-128.ibm.com/developerworks/xml/library/wa-ajaxtop1/
http://www-128.ibm.com/developerworks/xml/library/wa-ajaxtop2/