views:

15

answers:

1

I am a newbie to spring mvc annotations. Which is the best way to load data into a page that has say a string, list (list of country for a drop down) and some form elements like name and address

I see two possible ways

  1. @ModelAttribute on a method - can be used for string, list, initialize the form elements
  2. Put the string, list into ModelAndView in the RequestMethod.GET method

Can anyone shed light on the pros and cons.

Also can some one give a brief example on how to load reference data and form data. What goes inside a form backing object (just the form elements like an address object or the reference data also)

+1  A: 

I prefer to create a method which returns, for example, a collection of Strings. I annotate this method with @ModelAttribute, and then the data is always available to all of my vies.

You can manually inject into the model as you described, which is sometimes more appropriate (when you only need model attributes in very specific cases). Often you'll want certain model attributes for multiple views, so it's easier to use a @ModelAttribute-annotated method.

Here is a simple example I wrote on barebones Spring MVC which might help you get started.

James Earl Douglas