Thursday Dec 10, 2009

The Ajax Experiment

Ajax was not new to our team, however, we had not used it too extensively in our application in the past -- we generated bread crumbs, calculated when the browser needed to be reloaded, and populated tree nodes when server state changed.  To solve our performance problem, we decided to Ajax was our best hope (or going back to frames -- and that was NOT going to happen)....[Read More]

Thursday Feb 15, 2007

Woodstock is here!


Woodstock JavaServer Faces Components 

Sun has delivered some great components as part of Sun Java Studio Creator and NetBeans Visual Web Pack.  But if you don't use one of those products, you probably didn't know it.  Well, now you don't have to use these tools to enjoy the rich JavaServer Faces components they provide.  In fact not only are they available outside these products, they are now Open Source!

Project Woodstock is the project which contains the source code for these components.  Both Creator's and NB VWP components were derived from earlier versions of this code base.  The code is released under the very flexible CDDL license.  These components have been in the making for over 2 years and have gone through several development cycles.  They're stable, full featured, and work seemlessly together sharing a common theme -- they're ready for production!

Let's take a look at what some of these components look like:

  • The CommonTasks component produces a page of common tasks for your application, complete with drop-down help and nice roll-over visual effects:

Common Tasks


  • This Table component can create simple to very complicated tables.  It takes advantage of the unique "DataProvider" feature that makes managing your data efficient and a breeze!  Below is a fairly complicated table: 




  • The Masthead component allows you to present a visual appealing and very functional masthead complete with status information, buttons, branding, and more: 


  • The Wizard component allows you to create web-based wizards for walking your user through a process:


  • The Tree component provides a both a server-side and client-side rendering option and nice visual appeal: 


  • The PropertySheet and FileChooser shown below are two more nifty components that provides a nice page layout and the ability to browse and select a file on the server: 


All of these components are "themed" so you can adapt the look of these components to your company's look and feel by creating your own theme.  Many of the components use Ajax (via Ed Burn's Dynamic Faces project), and more Ajax features are sure to come.  The TLD document and example application included in the project provide more than enough information to get you started.

If you're a Faclets user, Jason Lee is already working on creating a Facelets taglib for Woodstock.  And of course if you're a JSFTemplating user, I have full support for the Woodstock components built in... plus I have a simple example app on the site to get you started.  Plus, the entire GlassFish admin console (source here) is built using JSFTemplating and Woodstock components.  So whatever your JSF environment (NetBeans, Facelets, JSFTemplating), you'll be able to use these components.

So what are you waiting for?  Go download the components and try them out!



Tuesday Jan 09, 2007

JSFTemplating + DynamicFaces = Easy Ajax

Ok, let's get right to it.  Ajax is hot, but it needs to be "real".  You need a fast way to be productive with Ajax, not gfx-clock productive, but real-world productive.  Let me offer a suggestion...

In an effort to keep my blog entries concise (part of my New Year's Resolution), I will get right to the point.  By using JSFTemplating with Ed Burns's DynamicFaces project, you have a perfect Ajax solution tailored to JSF's component architecture.  Add JSF widgets like those found Greg Murray's jMaki project and you have a complete solution.  However, I will save jMaki for a later blog.  I want to stick to the basics for this entry -- Keep It Simple.

I created a very simple example which shows all you need and little more.  You can download the complete war file and try it out.  Here are the contents of the .war file:

guess.jsf                                         <-- JSF page
WEB-INF/lib/dataprovider.jar                      <-- Sun component libraries & dependencies
WEB-INF/lib/jsftemplating.jar                     <-- JSFTemplating .jar file
WEB-INF/lib/jsftemplating-dynafaces-0.1.jar       <-- JSFTemplating/DynamicFaces integration .jar file
WEB-INF/web.xml                                   <-- deployment descriptor


This is a 1 page application -- doesn't get much simpler than that.  It also doesn't use any managed beans, or any custom Java code.  Instead it leverages JSFTemplating to do everything in the .jsf file.  Let's look at the page:

        Guess Demo Screenshot 


And the source:


    <sun:head title="Ajax Zone">
<jsfExt:scripts />

<sun:form id="form">

            <sun:markup tag="div" id="result">
                <staticText value="#{pageSession.msg}">
setPageSessionAttribute(key="msg" value="Guess my number [1-100]!");

            // The data input / processing...
<sun:textField id="guess" label="Your guess: " value="#{requestScope.guess}" />
<sun:button text="Submit AJAX Request" onClick="
{execute: + ',form:guess',
immediate: false,
inputs: + ',form:guess_field',
render: 'form:result' });
return false;

if ("#{requestScope.guess}>64") {
setPageSessionAttribute(key="msg" value="#{requestScope.guess} is too high!");
if ("#{requestScope.guess}=64") {
setPageSessionAttribute(key="msg" value="You WIN!!! It was #{requestScope.guess}.");
if ("#{requestScope.guess}<64") {
setPageSessionAttribute(key="msg" value="#{requestScope.guess} is too low!");



// This fields below exist to show required fields are NOT effected
<sun:textField label="This is required: " required="#{true}" />
<sun:button text="Submit (non-AJAX)" />


The blue area above defines an area where a prompt will be shown to the user.  The beforeCreate event allows an initial value to be set.  PageSession is a JSFTemplating-added scope.  The value bound to the pageSession variable msg will be shown to the user.  This is plain, oridinary text that will be replaced by an Ajax request.

The red areas is the JavaScript from DynamicFaces that is needed to submit the request.  In this case, I've specified several things for this Ajax request:

  • execute: A JavaScript String specifying which JSF component id's to process.  In this case the button itself and the field to submit.

  • immediate: Not really needed, but if there was validation on the field in "execute" immediate would skip the validation.  In this example it does nothing useful.  I added it to show that the other required field on the page truely isn't processed even when immediate is false.  This is true partial-page processing.

  •  inputs: The values sent to the server.  This may be different than the components processed.  In this case the HTML id of the form field is different than the component id because of how the componet is implemented, so I need to send "form:guess_field" for the value to be sent correctly during the Ajax request.

  • render: This specifies which area(s) of the screen should be updated with the response.  In this case the <div> around our message text.

Finally, the green area is the business logic.  It evaluates the submitted value and displays an appropriate response.  In real-world JSFTemplating applications, you'd likely put this in Java code in a handler such as is done by the built-in "setPageSessionAttribute" handler in this example.  But to keep the example simple (1 page), JSFTemplating's condition processing was used instead.

That's it!  The user types something in the box, clicks the button, an Ajax request displays the response.  The beauty is its simplicity.  You work with Ajax requests on the server-side exactly as you would with any other JSF action.  You glue it together with a small amount of JavaScript which, as you can see, gives you \*lots\* of flexibility on what is sent to the server, what is updated, etc.  So your next step is to look at your own application and see where you could improve your user experience by doing partial page updates, or other asynchronous checks.  The plumbing is in place... now use it!







« July 2016