Edit Multiple Records With Spring (and Velocity)

Consider an example web application that manages a database relationship between Movie Studios and Films. Each movie studio has n active films. Updated information on Films arrives in batches (box office numbers, ticket sales, etc). Searching, selecting and editing Films one at a time is inefficient, so the client has requested a "batch editor" interface.

We started from the user's perspective and decided a spreadsheet-style interface would be most efficient. Users will perform a search to get a list of Films, then edit them in the spreadsheet interface. This required data binding to multiple database records simultaneously.

Here's how I did it in Spring with a Velocity view.

For the most part, the data binding process works the same with multiple records as it does with one. I extended the SimpleFormController, with an implementation that retrieves a List of Films from the data layer in its formBackingObject() method.

That was working fine, so I began to code up the edit form. Here's an excerpt of the first (broken) attempt:

#foreach($film in $command)

  #set($index = $velocityCount - 1)

  #springBind("command[$index].boxOffice")

  <input id="$status.expression" name="$status.expression" value="$!status.value"/>

#end

That attempt did not work (received an error saying the object command[0] does not exist as an attribute). I scoured the forums and documentation and found no help. Everything seems to check out. I eventually had to look at the Spring source to find out why my code was not working. As it turns out, Spring's data binding assumes that the command object is a simple object (not a List, Map or other Collection). The special List and Map syntax:

<!-- Map -->
#springBind("command.map['key']")

<!-- List -->
#springBind("command.list[index]")

does not apply the command object itself. Knowing that, I changed my formBackingObject() method to return a command object (call it FilmBatch) that wraps a List of Film objects. I then changed the Velocity template to the following:

#foreach($film in $command.filmList)
  #set ($index = $velocityCount - 1)
  #springBind("command.filmList[$index].boxOffice")
  <input id="$status.expression" name="$status.expression" value="$!status.value"/>
#end

0 TrackBacks

Listed below are links to blogs that reference this entry: Edit Multiple Records With Spring (and Velocity).

TrackBack URL for this entry: http://aaronlongwell.com/mt/mt-tb.cgi/1

1 Comments

| Reply

Thank you, thank you, thank you....

thank you.

Leave a comment


Type the characters you see in the picture above.

Who's this guy?

Aaron Longwell is Chief Web Craftsman at New Media Logic Corporation in Coeur d' Alene, Idaho. As a professional software developer for 12 years and a student of public policy, he occasionally has interesting things to say about software, technology, culture and politics.

Subscribe to feed Subscribe to my RSS Feed

  • View Aaron Longwell's profile on LinkedIn
  • Recommend Me