пятница, 25 июля 2008 г.

Wonderful Wicket

All ingenious is simple

First, I'm going to tell you how I came across Wicket. A year ago I was working for Ciklum on a project that was basically a Java web application. The application consisted of hundreds of web pages. Although we were using Spring MVC, I didn't feel quite happy, do you know why ? The answer is "reusing a single component in different web pages is not perfect with Spring MVC". This is not weakness of Spring MVC, it's weakness of all workflow-oriented web frameworks. If you need to reuse a component with Spring MVC you need to

  1. Create a reusable tag which contains shared HTML code

  2. Create a reusable method which handles HTTP requests from the tag. It is not easy when you have many nested components on a single web page

  3. Call the method from an MVC controller for every page containing the tag

If you are intensively reusing components on GUI level , then a component framework would better suit your needs.

So I started my research on component web frameworks for Java. My first candidate was JSF. After I had read a book on JSF, I had impression that JSF is overcomplicated technology. Nevertheless I downloaded the latest release of ICEFaces and tried to write a simple "hello world" application with a singe button and a message area. I've spent two hours configuring it and six hours figuring out how to eliminate an exception. I can't remember the exact error, it was something about "invalid JSF context". I came to conclusion that I don't need such a framework. Developing GUI is probably the easiest task and definitely shouldn't be that complicated. Developing custom components for JSF is a nightmare. You would have to write three or four classes for a single component. One of the reasons why JSF is so complicated is that JSF has been designed to support different renderers' families. For example, you can switch from HTML to WML by changing a line in the config file. But I believe that in a couple of years WML will not be widely used, because the number of mobile phones capable of displaying HTML increases every year. Eventually I decided that I can't spend my precious time on mastering JSF.

So I said "No" to JSF. The two other candidates were Tapestry and Wicket. So I downloaded Wicket and started reading documentation. I was really impressed by the framework's simplicity. Configuration is very easy. All you have to do is to add Wicket's servlet to the web.xml.

Another concept I like in Wicket is separation of HTML and Java code, in other words separation of concerns. Imagine your technology is JSP and you have a team consisting of programmers and a web designer. In most cases the web designer has his own copy of GUI consisting of static HTML pages, so-called "prototype of GUI". If any changes to HTML code are required, then the web designer updates his web site and sends the new version of modified HTML files to a programmer. The programmer finds a single JSP or multiple JSP's to be modified according to the modified prototype. The programmer modifies JSP's. I believe that this approach is completely wrong. If you are using Wicket, forget about this slow and ugly way of modifying HTML. Just let the web designer to modify HTML, that's all. No programmer's assistance is needed to modify HTML, and I believe it's great !

If you are familiar with Swing, then mastering Wicket will be very easy for you. Spring and Wicket are very similar. Swing JPanel is like Wicket Panel, Swing Button is like Wicket JButton. Very simple. If you want to handle a form's submission, just add an event listener to the form's button, just like in Swing. Working example is available at http://www.wicketstuff.org/wicket13/echo

Wicket is different from javascript - oriented frameworks like GWT or Echo. GWT components are rendered with javascript, while Wicket's components are mainly pure HTML. Web pages built with Wicket components can be indexed by search engines while GWT pages can't.

Wicket integrates with Spring, has support for internationalization and security. If I were to start a new web project now, I would definitely choose Wicket.

Комментариев нет: