Just another weblog

  • Subscribe

  • Recent Posts

  • Pages

  • Archives

  • Top Clicks

    • None
  • Advertisements

Spring + DWR – Building Spring Services – Part 2

Posted by damuchinni on February 13, 2009

n the first post of this series I discussed the integration of AJAX-based applications with backend (server-side) logic in JAVA.  There are a variety of technologies to handle the complexity of AJAX/JAVA integration, and in my opinion Direct Web Remoting (DWR) is one of the best (if not the best) solutions.

In this post let me jump straight into a sample project that demonstrates the use of DWR with backend services developed with Spring.

For this project I am developing an application for managing bookmarks….kind of like a rudimentary implementation of  Essentially I can add, edit, delete and list bookmarks.  The front-end is implemented as an AJAX application using javascript with a little help from DWR, and the back-end is implementing using Spring.  As always…I’m using Skyway Builder, the open source Spring code generation tool (Eclipse-based), for developing the back end.  At the time I’m writing this post, the current version of Skyway Builder is 6.1.

Developing the back-end will take 5 minutes.  Assuming you have Skyway Builder 6.1 installed, here are the steps.

Step 1: Create Project

From the Skyway Navigator I created a new project called BookmarkManager, and I used all the default options.  When I create the project, two Eclipse projects will be created:

  • BookmarkManager – stores my modeling artifacts for my Spring application
  • BookmarkManager-Web – a standard Eclipse Dynamic Web Project that store my web application, including the code generated from my models in the BookmarkManager project

Step 2: Create Data Type

Right-click on the BookManager project, and create a new Data Type called Bookmark, the domain model for my project.  The following fields will be added to the data type:

  • id – ID – PrimaryKey
  • url – Text
  • title – Text
  • description – Text
  • created – Date and Time
  • tags – Text


Step 3: Scaffolding

Now I’m going to cheat a little.  While I can go ahead and manually create all the other Spring artifacts that I need, it’s a lot quicker for me to just scaffold an application from the data type.  By scaffolding Skyway will create a fully functional Spring MVC application for me, including the DAO, Services, and Controllers.  Now I don’t need the Controller for my bookmarking application, but I got it for free.  I’ll keep it for now, but I can always just delete it later.

To scaffold the data type, right-click on the Bookmark data type, and select Scaffolding–>Generate CRUD.

Let’s take a quick minute to review all the things that were created for me by the scaffolding function.

  • DAO with all necessary named queries
  • JPA Persistence with Hibernate as JPA Provider
  • Service with CRUD Operations
  • Annotated Spring Web MVC application with Controllers and Actions
  • All Spring annotations and configuration files
  • JSP pages to support CRUD application
  • JUnits for every Service


Step 4: Specify a DB connection

Skyway Builder has built-in validation, and you’ll notice by the red X on the BookmarkDAO that indicates that something is wrong with the DAO.  Open the DAO by double-clicking on the artifact, and you’ll see an error message stating that “BookmarkDAO must specify a connection“.  This is an expected error, and all you have to do is to select a database connection (in Database Configuration tab) for the DAO from the list of database connections you’ve configured in Eclipse.  In my case I’m using MySQL 5.1, and the name of my database connection is SkywayDS.  Save BookmarkDAO, and the red X goes away.


Step 5: Change the implementation of SaveBookmarkOperation…just slightly

The last step is to change the implementation of the SaveSaveBookmarkOperation operation just a bit.  This operation was scaffolded to simply persist the input object (bookmark) to the database using JPA and Hibernate.  The assumption is that all the required fields of the object (especially the primary key fields that uniquely identify the object) have been initialized, but for me that isn’t the case.  You see…I don’t ever want to expose the primary key to the end-user.  So I don’t expect them to be able to see it or set it.  So I need to change the implementation of this operation to initialize the id.  There’s a couple ways of doing it, and I decided to use Groovy, which Skyway Builder supports to compliment the generated code and Spring.  The Groovy Step is available in the Services drawer of the Palette.  By dropping the step onto the canvas, and I can then configure it with some Groovy script.


As you can see, I’m checking to see if the id is already set.  If the id is set, then I must be persisting an updated version of an existing bookmark.  If it isn’t set, then I must be persisting a new bookmark, and I need to generate a unique identifier for this bookmark using the guid() function.  While I’m at it, I also initialize the created field with the current date and time using the datenow() function.


Make sure you set the Groovy Step as the start step by right-clicking on the Groovy step and selecting “Start Step”.

That’s it.  The service layer of our application has been implemented, generated and it’s ready to run.  There’s no scripts to run, no additional configuration required, no Java code to write.  As you scaffold and/or model your application, the Spring code is generated and updated in real-time.

In the next post (part 3) I will cover how to add DWR support to the Spring Services, and in the final post (part 4) I will cover how the implementation of the AJAX-based front-end.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: