开发者

Spring MVC annotation preload form

开发者 https://www.devze.com 2023-01-11 16:57 出处:网络
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 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 m开发者_开发技巧ethod - 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)


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号