Matt RaibleMatt Raible is a Web Developer and Java Champion. Connect with him on LinkedIn.

The Angular Mini-Book The Angular Mini-Book is a guide to getting started with Angular. You'll learn how to develop a bare-bones application, test it, and deploy it. Then you'll move on to adding Bootstrap, Angular Material, continuous integration, and authentication.

Spring Boot is a popular framework for building REST APIs. You'll learn how to integrate Angular with Spring Boot and use security best practices like HTTPS and a content security policy.

For book updates, follow @angular_book on Twitter.

The JHipster Mini-Book The JHipster Mini-Book is a guide to getting started with hip technologies today: Angular, Bootstrap, and Spring Boot. All of these frameworks are wrapped up in an easy-to-use project called JHipster.

This book shows you how to build an app with JHipster, and guides you through the plethora of tools, techniques and options you can use. Furthermore, it explains the UI and API building blocks so you understand the underpinnings of your great application.

For book updates, follow @jhipster-book on Twitter.

10+ YEARS


Over 10 years ago, I wrote my first blog post. Since then, I've authored books, had kids, traveled the world, found Trish and blogged about it all.

Using DWR with Spring and Hibernate

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.

Posted in Java at Apr 28 2005, 02:10:26 PM MDT 31 Comments
Comments:

Well, your app looks cool. Is it possible to add dynamic table rows using DWR? I need this functionality in a current app. I decided to do a full JavaScript version because IE had issues with several of the Ajax libraries available. The advantage of a full JS version is that there's no need at all to go back to the server before submitting the form.

Posted by Lars Fischer on April 28, 2005 at 03:24 PM MDT #

Well, the movie looks very, very nice. IMHO, you sould really integrate DWR in AppFuse.

Posted by 217.185.82.43 on April 28, 2005 at 03:27 PM MDT #

That app is my new inspiration...

Posted by Jon Harper on April 28, 2005 at 04:04 PM MDT #

Lars - adding a table row can be easily done with the DOM by using element.cloneNode() and such. This is a good reference for working with the DOM. I did dynamic rows on a project a couple years back and it did work well - but was a bit complicated after I added client-side sorting.

Posted by Matt Raible on April 28, 2005 at 04:18 PM MDT #

Wow. That movie is impressive, very nice little app. Beautiful implementation of FAT! Encounter any problems? (Other than the obligatory IE burp)

Posted by Adam Michela on April 28, 2005 at 08:06 PM MDT #

Adam - I'm glad you asked. I encountered two issues: 1) creating radio buttons in IE and 2) trying to call the same method with different XHR calls. I solved #1 by using a checkbox that acts like a radio button. I solved #2 by splitting it into two methods with *lots* of duplicate logic. To truly solve #2, it would be nice to have some sort of synchronized keyword for a function.

Posted by Matt Raible on April 28, 2005 at 08:54 PM MDT #

[Trackback] Thanks to forgetfoo I am alerted to more discussion on the whole AJAX phenom. I do agree that it is cool stuff and it's coming down the pipe. First a discussion from Janice at adaptive path and a follow up from kottke of, well, kottke fame. Both are r...

Posted by My Own Little World on April 28, 2005 at 09:09 PM MDT #

[Trackback] Matt Raible has written up his experience using DWR with Spring and Hibernate. Matt also kinda put up a movie showing the Ajax features at work. Very nice and clean. A good view of DWR, and nice Ajax techniques (e.g....

Posted by Ajaxian Blog on April 29, 2005 at 12:27 AM MDT #

I was unable to get the movie to play in either VLC or Quicktime. I am running Win XP SP2, not OSX.
Do I need a special codec?

Quicktime simply says that a "required compressor" is missing.

VLC's error log: "no suitable decoder module for fourcc `rle '."

Thanks.

Posted by foo on April 29, 2005 at 02:09 AM MDT #

Matt, thanks for the hint with cloneNode! I checked two samples using a similar approach, one of them is iEat (ieat.sf.net). The problem is that none of the scripts I tried do support IE properly.

Posted by Lars Fischer on April 29, 2005 at 02:22 AM MDT #

foo - I tried it on my Windows XP SP2 box with both IE and Firefox and was unable to reproduce your problem.

Posted by Matt Raible on April 29, 2005 at 07:32 AM MDT #

Nice work Matt! I started using DWR recently and found the dynamic table a problem. This was only because I needed to change the text color per row depending on what type of information it was holding. I ended up doing my own drawTable funtion and used innerHTML since IE wouldn't style the newly added elements via the createElement manipulations. Using innerHTML was a pain but, I think JST would be a good problem solver for that.

Posted by John Christopher on April 29, 2005 at 11:56 AM MDT #

Thats really impressive. Good stuff..

Posted by Sanjiv Jivan on May 01, 2005 at 07:14 AM MDT #

[foo said:I was unable to get the movie to play in either VLC or Quicktime.]
Got it to work.

[foo said:Do I need a special codec?]
I had Internet Explorer disabled, when I re-enabled it, quicktime made a call for a codec through IE. I had the minimal install of Quicktime, so it would seem the codec is not present in the minimal setup. Not sure why Quicktime was unable to find Firefox.

[Matt Raible said:I tried it on my Windows XP SP2 box with both IE and Firefox]
I only tried to play it from local disk - never used the plugins. So, unsure if using the browser plugins would have made a difference.
Nice work on the app!
cheers

Posted by foo on May 01, 2005 at 12:03 PM MDT #

Now web applications really look like traditional desktop apps. It's only sad that there is the browser around the app. Would that have any impact on the development of desktop applications? Maybe this kills terminal servers for applications inside larger corporations?

Posted by Stephan Schwab on May 01, 2005 at 03:53 PM MDT #

For Ajax check out also this tool: http://www.servletsuite.com/servlets/jscalltag.htm

Posted by Dan Novik on May 02, 2005 at 06:50 AM MDT #

This question is from a newbie point of view: How do you apply CSS styles to the selection color of the selection boxes elements of your forms?. I have tried with option.test:hover {...} but does not work. Thanks in advance.

Posted by Paul on May 03, 2005 at 07:33 PM MDT #

I noticed that just after this blog entry was made Joe Walker updated DWR to version 0.6beta. From his blog page it looks like it just added support for commons-logging. BTW, is the code for this Struts-Spring-Hibernate-DWR application going to become available? I've been thinking of using DWR in an Spring application which archives and distributes a collection of documents from a government agency. The branches produce a calendar which lists when documents will be released. I thought DWR would fit really well to produce an interactive calendar which displayed the list of documents as a user selected a year/month/day on the calendar. Anyone doing anything like that?

Posted by John Fereira on May 04, 2005 at 06:44 AM MDT #

Paul - AFAIK, this is not possible. The "hover" color you see in this video is specified in my operating system settings.

John - this app is something I developed internally for a client. It will not be made available publicly.

Posted by Matt Raible on May 04, 2005 at 10:47 AM MDT #

Matt: Thanks a lot by the data ... anyway great color combination :-P

Posted by Paul on May 05, 2005 at 11:24 AM MDT #

Matt, about the lazy-loading issue, could you send me more details, I'm facing that problem with some attributes of my bean that I don't need to be initialized... Is there any way to configure DWR to just 'populate' some attributes? Tks!

Posted by Cristiane on June 23, 2005 at 11:13 AM MDT #

Cristiane - I basically had to use a simple NameValue object to prevent DWR from trying to instantiate lazy-loading on my Hibernate POJOs. There have been recent fixes to the latest release of DWR, so you might want to read some discussions on the mailing list. Hope this helps.

Posted by Matt Raible on July 11, 2005 at 11:00 PM MDT #

On the lazy-loading issue, there are a number of Java-to-Java object mappers that might help. One I've worked on is XSnapshot, which is a set of XDoclet tags/tasks for creating objects like NameValue declaratively from markup in the POJO. This is geared towards DTO generation for remoting in general (EJB, SOAP, etc.), and DWR/AJAX is just using another kind of remoting.

Posted by Bill Schneider on August 03, 2005 at 08:46 PM MDT #

Matt can you please direct me where I can find you app using DWR, Spring and Hibernate. I cannot find a link to it. I am interested to check out how it works and if there are some lessons learnt. Pretty much want to use the same technology. Thanks.

Posted by peter on May 07, 2006 at 08:03 PM MDT #

Peter - the application shown in this video is not open source. I wrote it for a client last year.

Posted by Matt Raible on May 07, 2006 at 09:53 PM MDT #

There are some good online tutorials on introduction to ajax at www.kynou.com. These tutorials are good because they are like training simulators. I hope this is useful to you guys :)

Posted by Eric Viegas on May 10, 2006 at 11:29 AM MDT #

Matt, As usual, your works serve as a great inspiration for me. I've just went through the DWR tutorial in your wiki and it worked like a charm. I'm eagerly looking forward to start using Ajax in Internet Polyglot and other projects.

Posted by Misha Gavryuchkov on July 13, 2006 at 04:06 PM MDT #

HI, Visit: http://www.roseindia.net/struts/hibernate-spring/index.shtml This tutorial shows how to develop User Registration and Login Application using Spring, Hibernate and Spring technologies. Regards Deepak Kumar

Posted by Deepak Kumar on November 19, 2006 at 10:22 AM MST #

its great...

Posted by k jagdishchandra on March 24, 2007 at 03:39 AM MDT #

DWR and dynamically table editing examples are good but, cell creation functions is not working well if you add the dynamic content as html text in each cell content function instead you have to add DOM tree objects/fragments to make it work. I am sure this is happening because I am overriding the default cellCreator function. Because given example was just using the default cellCreator function. I think this is very important to be aware of. Deha Peker

Posted by Deha Peker on June 08, 2007 at 10:54 PM MDT #

Hi, your article pointed me in the right direction few months ago, so here comes my small contribution;

regarding this: "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."

I think a better solution, almost for lazy progammers like me, is excluding lazy-loaded children properties:
<convert converter="bean" match="com.example.Fred">
  <param name="exclude" value="property1, property2"/>
</convert>

Posted by tonig on February 09, 2008 at 09:11 AM MST #

Post a Comment:
  • HTML Syntax: Allowed