First Steps With ZK - AJAX For Java

    ZK, a fairly cool name for what I feel is a pretty cool AJAX framework. Having done AJAX development for a few years now, I have yet to find an AJAX framework that I liked/found usable . . . until now. Is it that great? Well, for me it was almost exactly what I was looking for, but it also has it's caveats. In this article I intend to share some of the lessons learned and finish up with an overview.



    Let's start off with installation. To install ZK, you first need to download the ZK libraries. They are all packaged and available for download from Source Forge, but to find them easily, go to the ZK open Source Web Site HERE. As far as I can tell, you install ZK pretty much the same way you install any other Java Servlet library, by putting the files in the Servlet Engine's lib directory, or in the WEB-INF/lib directory of the application. Beyond that, there are a few configuration settings to be placed into the web.xml, but those details can be found in the ZK Quick Start Guide


    Once I had ZK installed, I went straight to the documentation. There is quite a bit of it on the ZK site. I already had a project in mind, so I started searching for what it would take to complete the project using ZK.

    One of the features I would need most is an auto-complete text input box. ZK had that covered with their ComboBox element. The ComboBox as packaged is designed to allow defined options and user input. The problem I had is that I wanted the suggestion list to come from a database, so I went to the documentation. In the documentation there is a great deal of discussion about the SimpleListModel object as a data binding for the ComboBox element. The up side is that if you use Hibernate, this looks like it would work fairly easily. The down side is that I have never had much luck with Hibernate being reliable, so I wanted to use standard Tomcat DBCP/JDBC. There's nothing in the documentation about this, but after searching I found THIS page which explains how to extend the ComboBox with your own Java class and that worked PERFECTLY. I merely modified their example class to use database information instead of their static dictionary. After extending the class, you can make the ComboBox XUL tag make use of the new class by specifying it with the use attribute as shown below:

<combobox id="myBox" use="com.blogspot.MyComboBox"/>


    On to the next hurdle, which in my case was authentication. I have yet to make the move to more advanced authentication frameworks like Acegi, so I still use JNDI Realms in Tomcat. The problem I ran into is that there appears to be no way to determine role memberships from within the ZUL markup files! I headed back to the documentation and found bupkiss. There's not one mention of authentication Realms in the ZK documentation. The only documentation I found about Servlet authentication was specific to Acegi and JBoss. Next stop, the source code, and what'd'ya'know, I found it. From within a zscript block, there is an object called execution. One of the member methods of execution is called isUserInRole!!! Perfect, just like I would use in a standard Servlet/JSP application in Tomcat. By using a little zscript and the if/unless attributes of the elements in the ZUL file, I was able to specify which groups could access which portions of the application. Excellent, but it would have been better if I could have found this information in the documentation rather than spending 6 hours reading the source code for the framework.

    I still have a few problems I have yet to overcome with ZK, like figuring out how to trigger events from a select-box change, but overall it is very intuitive. The project which I embarked upon was to write a management application for our SQL based e-mail system; and from first reading about ZK to having a completed application took only 2 days. That's a whole lot better than the progress I have made with other AJAX frameworks, and I didn't have to dirty my hands with JavaScript at all!!



    Let's wrap up with a list of Pros and Cons:

Pros


  • Pretty easy to install

  • Integrates well with Java

  • Requires NO JavaScript knowledge to make useful applications

  • Cross-browser compatible

  • Fast

  • Eclipse plugin for ZK development



Cons


  • Poor and sometimes non-existent documentation

  • The ZK Studio plugin for Eclipse seems to have a memory leak and has caused Eclipse to hang several times while I was using it.


    Overall, the best AJAX framework I have used to date. I like it better than Prototype, better than DWR, better than GWT, and better than Dojo. Most importantly, I like it better than the results of hand coding JavaScript to integrate with custom Java code, which is what I usually had to resort to with other frameworks. Give it a try, and let them know that they need to improve the documentation.



Agree? Disagree? Leave a comment...

Comments

Unknown said…
Hi there,
This is Robbie Cheng from the ZK Team. It's an encouragement for us to hear voice like you.
Regarding your question, simply register onSelect event listener on combobox for further procession.

By the way, are you interested in writing an article about how did you handle authentication problem with ZK? We can publish it on ZK website.

Please contact me at robbiecheng at zkoss dot org.

thanks!
Unknown said…
Hi Deven,

First of all, let me say that I agree 100% with your comments; ZK is a great framework but with respect to the authentication issue, well, I feel it is simply lacking a real world solution.

I am busy evaluating ZK, and one of the requirements I looked into was the issue of authentication and authorization.

The solution that you mention about using "isUserInRole" seems tedious if you were to have a large number of pages. Testing isUserInRole for a large project just doesn´t seem

I am busy looking into the possibilty of developing a solution using a Filter which will 1. Ask the user for authenticate and 2. verify authorization based on requested URI's.

I am trying to achieve this by overriding the ServletRequest object in the Filter by using a wrapper as mention in the Java EE tutorial, dispatching the request causes problems (if you want to use zul login page) since it bypasses zk initialization.

I would apreciate your comments...

Regards,
Jorge Cercas

Popular Posts