views:

1550

answers:

4

Does Spring 2.5 (or 3.0) include support for dynamically populating a select list based on what the user selects from another form element?

For example if you have a form with 2 select for (Car) Make and Model. When the user selects a Make from the first list, the Model select should get populated with the available Models for that Make.

I can do it 'manually' using jquery/Javascript but was wondering whether there was any functionality available in Spring MVC to reduce the required leg work.

A: 

Spring MVC is really only the controller, so no it doesn't support this sort of functionality.

The way I'd do this would be to use JQuery and make an AJAX call to the server to get the list of models when the make of car is selected, then dynamically populate the select box.

Nick Holt
Spring MVC is a bit more than just controller, and already includes some support for client side javascript.I know I can do it myself with javascript, but am interested in whether Spring has implemented support for it.
objects
It's been a year or so since I've used it, guess I'm a little out of date. Brings back bad memories of Struts 1 when I start seeing frameworks for managing forms. Since those days I've always preferred to use Javascript to retrieve XML from the server which I use to dynamically populate forms.
Nick Holt
A: 

This is not dynamic and will reload the complete page but you can do this with only Spring MVC by implementing the update as a form change request. This requires overriding isFormChangeRequest and onFormChange in your SimpleFormController.

Mark
Hi Mark - isn't SimpleFormController reponding to form posts and subsequently refreshing the whole page in the browser?
Nick Holt
That is true I will update my answer to explicitly mention this.
Mark
I'm using 2.5 annotation based controller
objects
+1  A: 

This is rather about the view and strategies how to populate it. So there are two strategies you could apply:

  1. Do a real server roundtrip and evaluate the value given by the first dropdown box to populate the second one. This can be done by the very basic Spring MVC means (either isFormChangeRequest of legacy inheritance based controller model or simply provide a method mapped with @RequestMapping in the annotations based model.
  2. Use a JavaScript library and provide a dedicated URL to just read the values for the second box depending on the value of the first box. JQuery is probably a good start but you also might take a glance at SpringJS (contained in the Spring WebFlow distribution).

You see, actually it's very much a question on how conservative you are regarding the use of JavaScript, Server roundtrips or the amount of data to go over the wire.

Oliver Gierke
Would you know specifically what support SpringJS provides with respect to populating select lists?
objects
I haven't done much besides skimming through the refernce docs, but there seems to be support for AJAX request built in: http://static.springframework.org/spring-webflow/docs/2.0.x/reference/html/ch11s04.html
Oliver Gierke
Its not really an ajax request I'm interested in. I'm more interested in the actual populating of the second select when the value changes in the first. Will have a closer look at SpringJS though, thanks.
objects
A: 

Spring does not yet offer any support for dynamically populating lists based on the selection in another list. The only client side javascript features are provided by SpringJS which includes support for form element decoration.

objects