JSFTemplating + DynamicFaces = Easy Ajax
By paulsen on Jan 09, 2007
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 pageWEB-INF/lib/dataprovider.jar <-- Sun component libraries & dependenciesWEB-INF/lib/json2.jarWEB-INF/lib/webui-jsf.jarWEB-INF/lib/webui-jsf-suntheme.jarWEB-INF/lib/jsftemplating.jar <-- JSFTemplating .jar fileWEB-INF/lib/jsftemplating-dynafaces-0.1.jar <-- JSFTemplating/DynamicFaces integration .jar fileWEB-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:
And the source:
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.
- 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.