| By Jim Driscoll | Article Rating: |
|
| December 10, 2009 01:30 PM EST | Reads: |
1,203 |
JSF 2.0 makes AJAX pretty easy - but it can't hide everything from you... It's tempting to just add a few AJAX tags into your page, and not worry too much about interactions - here's one example of a problem you may run into.
Let's say you've got a page with an input text, and a command button - like this:
1 <h:form>
2 <h:inputText value="#{blah.blah}">
3 </h:inputText>
4 <h:commandButton/>
5 </h:form>
Now, we decide to add an ajax tag:
1 <h:form>
2 <h:inputText value="#{blah.blah}">
3 <f:ajax event="blur"/>
4 </h:inputText>
5 <h:commandButton/>
6 </h:form>
Can you spot what's wrong with this example? When we push the button, we're also blurring the inputText. That means that the ajax request is sent - but then, almost immediately, that request is canceled as the whole page is reloaded.
Is this a bad thing? For this simple example, not so much. There's going to be a broken connection - and that can be a grim problem for a large server, especially if you start getting one on each page, for each use.
But the real issue is that you've just set up a race condition. Imagine instead you did this
1 <h:form>
2 <h:inputText value="#{blah.blah}">
3 <f:ajax event="blur" listener="#{bean.somethingthatchangesstate}"/>
4 </h:inputText>
5 <h:commandButton/>
6 </h:form>
Now we've got a real problem from that race condition - did the listener execute? Maybe. Maybe is never a good answer in software.
So - what to do?
Probably the best solution is also the simplest:
1 <h:form>
2 <h:inputText value="#{blah.blah}">
3 <f:ajax event="blur" listener="#{bean.somethingthatchangesstate}"/>
4 </h:inputText>
5 <h:commandButton>
6 <f:ajax render="@form">
7 </h:commandButton>
8 </h:form>
Switching to ajax for the commandButton will now provide a predictable call sequence.
One more issue: When the two connections are submitted simultaneously, an error alert may be produced. I just updated that error to say: "The Http Transport returned a 0 status code. This is usually the result of mixing ajax and full requests. This is usually undesired, for both performance and data integrity reasons." What happens if you want to do this? Well, the error alert only shows up under two conditions, both of which must be true - the Project Stage must be Development, and there must be no error listener set. So, if you're really sure you want to mix ajax and full requests, despite what I said above, just set up an error listener for your ajax code - you'll want to anyway for a production environment.
As always, if you have questions, please ask in the comments.
Published December 10, 2009 Reads 1,203
Copyright © 2009 Ulitzer, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
More Stories By Jim Driscoll
Jim Driscoll has worked at Sun Microsystems for 12 years, working on such projects as the first version of Servlets (in the Java Web Server), and the initial implementation of Java 2, Enterprise Edition. He is currently a Senior Engineer working on the implementation of Java Server Faces, helping to integrate technologies such as AJAX and Comet into the new release.
- Current Trends in the Data Management Market
- Open Systems Provide Way to Exit Money Pit
- Ulitzer - iPad for Business?
- Cybercrime, the Easy Way
- Nimbuzz Announces First Revenue-Generating Partnership With Largest Indonesian Operator, Telkomsel
- Data Services Update for .NET 3.5 SP1
- Actuate Invites Developers to Get BIRT-y with Flash and Flex
- X Marks the Games
- New Decade, Same Threats?
- Intro to Querying Lists with REST and ListData.svc in SharePoint 2010
- F5 Joins NetApp Alliance Partner Program
- VM Sprawl is Bad but Network Sprawl is Badder
- Current Trends in the Data Management Market
- Google Voice On The New Google Phone
- LG Lotus Elite and LG Rumor Touch Exclusively from Sprint
- Skiff and Sprint to Preview First Skiff Reader
- Open Systems Provide Way to Exit Money Pit
- Ulitzer - iPad for Business?
- Cybercrime, the Easy Way
- Progress Scoops Up Savvion in BPM Race
- Nimbuzz Announces First Revenue-Generating Partnership With Largest Indonesian Operator, Telkomsel
- Pearl Harbor, Punchbowl and My Grandparents
- Data Services Update for .NET 3.5 SP1
- Optimize Prime: The Self-Optimizing Application Delivery Network
- Are you Application vAvailable?
- 3rd International Virtualization Conference & Expo in NYC to Present a World Class Faculty
- Exclusive Q&A with John Goodson, VP & GM of DataDirect Technologies
- Stylus Studio 6 From Progress Software, An Integrated Tool With Breadth
- XQuery Adoption Rate Soaring Among XML Developers
- Configuring WebLogic Server 9.x JDBC
- DataDirect Predicts 2005 Will Be XQuery's Year
- XML Developer Stylus Studio Offers Release 2
- XQuery Co-inventor Speaks Out
- XQuery 1.0 Reaches Official Recommendation Status with W3C
- DataServices World: The Importance of Middleware and Data Services
- Stylus Studio 2006 Release 2 Now Available For XML Integration



















Ulitzer content is offered under Creative Commons "Attribution Non-Commercial No Derivatives" License.
For any reuse or distribution, you must make clear to others the license terms of this work.
The best way to do this is with a link to this web page.
Any of the above conditions can be waived if you get written permission from Ulitzer, Inc., the copyright holder.
Nothing in this license impairs or restricts the author's moral rights.