As I’ve mentioned here before, I am a huge fan of the Spring Framework, a big chunk of Java code that makes it easier to build applications. Spring’s main feature is enabling you to manage dependencies among components in your application using something called dependency injection. It also provides an MVC framework for building web apps (and interfaces to make it easier to use lots of other common frameworks). Anyway, until this week, my relationship with Spring has been nothing but good. Then I ran into an obscure problem that’s turning out to be a bear to fix (or work around).
In some ways, Spring is a little bit like Ruby on Rails, in that it makes some assumptions about how things ought to work so that the developer doesn’t have to deal with those issues explicitly. For example, Spring turns Hibernate’s checked exceptions into runtime exceptions. (The Hibernate developers agreed, and made the change themselves in Hibernate 3.0.) Anyway, to make a long story short, Spring is too smart for its own good when binding values from an HTTP request to a Java bean. If the property of the bean is a string array, and the incoming parameter is a single value, then Spring helpfully treats the incoming string as a comma-delimited list.
So, if you have a list of values come in (as if from a checkbox group), like “red” and “blue,” the resulting array has those two values in it. If two values like “Boston, MA,” and “Providence, RI” are submitted, then they are copied into the array as is. But if one value is submitted, “Tempe, AZ,” then Spring turns it into an array of two values, “Tempe” and “AZ.” The problem is that I can’t turn it off, even though I’ve been trying to figure out how all week. I’ve posted on the boards, read the code, stepped through it with a debugger, and still, no dice. The honeymoon is over.