Spring Framework

Multiple Selection Binding for Complex Objects in Spring

I ran into an issue where I needed to offer a multiple select interface to edit a many-to-many association of one domain object to another (assigning multiple roles to a user).

This JRoller Post helped me solve the issue.

Using Velocity Toolbox Features in Spring

Today the need to URL encode some URLs within a Velocity view came up in a Spring application of mine. After a few failed attempts to get a LinkTool working, I found the issue. There was a problem with the formatting of my toolbox.xml file. I thought I'd better recap the complete setup for using Velocity toolbox views in Spring.

First, I define a VelocityViewResolver in my servlet beans configuration file (spring-servlet.xml). I define a
toolboxConfigLocation, and set it to /WEB-INF/classes/toolbox.xml. I change the viewClass to use the VelocityToolboxView class instead of VelocityView. Here's an excerpt of the important area:

Nested Data Binding Customization in Spring

As covered elsewhere, Spring's MVC databinding can be customized using the ServletRequestDataBinder's registerCustomEditor methods. In most cases, you specify just the data type or a combination of data type and field name (for field-specific data binding rules).

I ran into trouble trying to customize the data binding of a nested Collection component's field. It turns out it's just as easy, just not very intuitive.

To register a custom PropertyEditor for a nested field, you use the same method as a non-nested field, but you use a special syntax for the field name. The following is a hypothetical example binding an Order object. The Order contains a field called customer, which is of type Person. The Person class has its own field birthDate.

Custom Date Formatting with Spring Data Binding


protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception
{
super.initBinder(request, binder);
binder.registerCustomEditor(Date.class,
"estimatedCompletionDate",
new CustomDateEditor(new SimpleDateFormat( "M/d/yy"), true)
);
}

Data Binding to Multiple Records in Spring (with Velocity)

A client came to me recently asking for modifications to their web application. The application has a many to one relationship; we'll call it the "one" side a Studio, and the "many" side's elements "Films". Updated information for films (box office numbers, tickets sold, etc) comes in batches. Searching, selecting and editing Films one at a time is inefficient, so the client requested batch editing ability.

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.