Jan 11, 2010

Potential Gotcha When Upgrading Spring MVC

After upgrading Spring and Spring MVC to version 2.5.6 (from a now-ancient 2.0.2), we started seeing some bizarre JavaScript errors for an otherwise-working application. This same problem may effect you.

The problem appeared when we had a form-backing object that held a list of child objects and some complex JavaScript for interacting with the form. Spring’s form tags starting at version 2.5 treat collections differently than in older versions, although this wasn’t apparent at first.

If you had a form-backing object with, say, a list of cars, you might have a form item in a JSP like the following:
<form:hidden path="cars[0].milesPerGallon" />
In prior versions of Spring MVC, this would output:
<input id="cars[0].milesPerGallon" name="cars[0].milesPerGallon" type="hidden" value="whatever" />
Note that the array-like syntax is necessary for binding the form data when the user submits the form.

(As an aside, setting the name and the ID to the same value causes problems in older versions of Internet Explorer.)

If you then accessed this element from JavaScript, you might have code like the following:
var elem = document.getElementById('cars[0].milesPerGallon');
After upgrading to Spring 2.5.x, though, the output differs:
&lt;input id="<b>cars0.milesPerGallon</b>" name="cars[0].milesPerGallon" type="hidden" value="whatever" /&gt;
The ID attribute no longer sports square brackets. This change breaks the JavaScript code that expected a different ID. And, with JavaScript’s habit of silently failing, this proved difficult to track down, as you don’t expect a change in your Java libraries to suddenly break your JavaScript.

This change was not done to make the ID and name differ. Instead, square brackets are not allowed in IDs in HTML, as described in http://www.w3.org/TR/html4/types.html#type-name. Spring MVC was changed to correct the IDs. Oddly, you are not supposed to use square brackets in the name attribute either, but the form tags did not change the output of the name attribute.

See http://jira.springframework.org/browse/SPR-2380 and http://jira.springframework.org/browse/SPR-4698 for more on the explanation for the change in Spring MVC.

About the Author

Eric Foster-Johnson profile.

Eric Foster-Johnson

Principal Technologist

Eric has decades of industry experience in designing and developing complex enterprise software, including designing and developing Grails and Java EE solutions to tough client problems. He has experience leading development teams, mentoring developers, and helping troublesome projects get back onto a success track. He has lead teams in both traditional and agile settings.

Leave a Reply

Your email address will not be published.

Related Blog Posts
Natively Compiled Java on Google App Engine
Google App Engine is a platform-as-a-service product that is marketed as a way to get your applications into the cloud without necessarily knowing all of the infrastructure bits and pieces to do so. Google App […]
Building Better Data Visualization Experiences: Part 2 of 2
If you don't have a Ph.D. in data science, the raw data might be difficult to comprehend. This is where data visualization comes in.
Unleashing Feature Flags onto Kafka Consumers
Feature flags are a tool to strategically enable or disable functionality at runtime. They are often used to drive different user experiences but can also be useful in real-time data systems. In this post, we’ll […]
A security model for developers
Software security is more important than ever, but developing secure applications is more confusing than ever. TLS, mTLS, RBAC, SAML, OAUTH, OWASP, GDPR, SASL, RSA, JWT, cookie, attack vector, DDoS, firewall, VPN, security groups, exploit, […]