For the past few weeks, I've been developing an application using Struts, Spring, Hibernate and the DWR project for my XmlHttpRequest framework. As you might remember, I used JSON-RPC for Ajax stuff on my last project. I found DWR to be much more full-featured and easier to use. This post is meant to capture some issues I encountered so others won't have to jump the hurdles that I did. For those of you that get bored quickly, here's a movie (QuickTime) of the app's Ajax features.
I've been using version 0.4 of DWR, and I haven't had a chance to try out version 0.5. When I first started using it, I ran into a ThreadDeath problem that was easily resolved by changing a log.debug message to System.out.println. I tried to reproduce this issue yesterday and couldn't, so who knows what that was all about. As far as configuring DWR in your webapp, that's pretty easy to do, and well documented. See the project's documentation or this Spring MVC HowTo.
Here are a few things I remember from my development experience.
- The examples are great, especially how to dynamically edit a table.
- When developing, make sure to set the "debug" init-param to "true". This allows you to go to http://location:8080/yourapp/dwr and see a screen that allows you to call methods on your exposed classes.
- In WEB-INF/dwr.xml, you need to specify a converter for each POJO you want to expose to your UI via JavaScript. I started out by converting a whole package, but found this to be *extremely* slow (we have a package of around 50 DTOs). So I changed it to be only the DTOs I was using. This turned out to take about 30 seconds to do the conversion, and was again unacceptable. The problem turned out to be that the converter was invoking all the lazy-loaded children for each DTO. My final solution was to create a NameValue object and only convert that. Then in my Spring bean, I populate it from DAOs and DTOs. I'm using Spring's OSIVF for Hibernate to ensure that DWR doesn't invoke lazy-loading.
- I had to override a few of DWR's JavaScript functions in util.js b/c they didn't work for me. I changed showById() and toggleDisplay() to use style.display='' instead of style.display='block' b/c this is what I've always used and block doesn't work that well. I also changed useLoadingMessage() to have a cleaner-looking load message.
- I used the Fade Anything Technique in this project and found that IE likes to have full 6-digit hex values for colors in CSS rules. The shorter 3-digit hex values simply don't work in IE.
- Using "test" buttons that only showed up for my username proved to be a great way to test the UI and the Ajax stuff. These buttons called a number of JavaScript functions to drive the UI and wait between invoking different functions using window.setTimeout.
All in all, using DWR was a great experience and I definitely plan to use it more in my projects. The client loves the app - especially since it's wicked fast and seems to work like a desktop app.
There's an annoying thing that currently happens with the Java code on my wiki: selecting code to copy doesn't work in Firefox (try it). Any easy workaround is to use IE. However, I found a solution yesterday after communicating with the author of the Java2HtmlPlugin for JSPWiki. His solution was to upgrade to the latest version (4.1) of the library. I did the upgrade yesterday morning, tweaked a couple of stylesheets and called it good. The copying was working and everything seemed to be fixed.
In the last 24 hours, this site has crashed around 8 times - seemingly every two hours. This is strange b/c it's been up a week at a time prior to that, as well as the fact that my ISP has scripts to restart Tomcat if it's not responsive. The only thing I changed was the java2html.jar, so I'm guessing that was the problem. I backed out the change this morning - and copying with Firefox is going to suck once again. Hopefully that change fixes the stability of this site.
Update: This site crashed 3 times today even after I backed out yesterday's change. It must be something else. It's wierd that it's crashing now after having several weeks of excellent stability.