0

I have noticed in many tutorials and other places that a default model attribute named "SpringWeb" is used to map all jsp elements to a java POJO when the form is submitted.

For example, in the following tutorial, @ModelAttribute("SpringWeb") is mapped to the Student object.

@RequestMapping(value = "/addStudent", method = RequestMethod.POST)
      public String addStudent(@ModelAttribute("SpringWeb")Student student, 

   ModelMap model) {
      model.addAttribute("name", student.getName());
      model.addAttribute("age", student.getAge());
      model.addAttribute("id", student.getId());

      return "result";
   } 

@RequestMapping(value = "/student", method = RequestMethod.GET)
   public ModelAndView student() {
      return new ModelAndView("student", "command", new Student());
   }

http://www.tutorialspoint.com/spring/spring_mvc_form_handling_example.htm

Even if a "new ModelAndView("student", "command", new Student())" object is not returned by the GET method as in the example, the "SpringWeb" model attribute is still present when form is posted back. For example - in the GET request if I write model.addAttribute ("xyz", "123) and just return the view name and I don't map the form fields using "path" in jsp tags. The form fields will still map to the java object via "SpringWeb" when the form is posted back.

There are no search results for "SpringWeb" in github too: https://github.com/spring-projects/spring-framework/search?utf8=%E2%9C%93&q=SpringWeb&type=

I do understand that custom model attributes can be used and mapped to the form elements instead of using @ModelAttribute("SpringWeb"). But, I could not find any related questions in stackoverflow which could explain in detail about what "SpringWeb" is and whether it is a good or bad practice to use it?

I debugged the model object on a "POST" request and found that "SpringWeb" was present as a "key" and "value" pair automatically in the "BindingAwareModelMap" model object. But, I could not find any documentation related to "SpringWeb" in the below link.

https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/validation/support/BindingAwareModelMap.html

What is "SpringWeb"? My current understanding is that it is a "catch-all" model attribute which is mapped by spring automatically when form is submitted. If yes, then how was it discovered and decided to use it without any documentation? (at least I could not find any)

Stacky
  • 503
  • 2
  • 6
  • 23

2 Answers2

1

the form-tags default modelattribute is command. in the referenced example the "magic" happens there:

@RequestMapping(value = "/student", method = RequestMethod.GET)
public ModelAndView student() {
    // the second and third parameter:
    // a new Student is binded to the form-tags default modelattribute
    return new ModelAndView("student", "command", new Student());
}

the second and third parameter of the ModelAndView's Constructor are responsible for binding a new Student-Object to the form-tags default modelattribute "command"

in the post-receiving-method, the name-value of the modelattribute-annotation "SpringWeb"

@ModelAttribute("SpringWeb") Student student

can also be removed

@ModelAttribute Student student
  • Even if you don't return a new ModelAndView ()...the "SpringWeb" model attribute is still present when form is posted back. For example, in the GET request do model.addAttribute ("xyz", "123) and return the view name. When you POST the form back, the form field will still map to the Student object. Why and what is 'SpringWeb"? – Stacky Apr 06 '18 at 11:38
  • In the case mentioned in my comment, the mapping of "path" in jsp tags is also not required. So, I mean to suggest is that "SpringWeb" is somehow independent of returning a model and view with command name. I have also updated my question to remove this confusion. – Stacky Apr 06 '18 at 12:07
  • your understanding of a "catch-all" model attribute seems to be right and i cant find any documentation too. i searched the spring-top-level project https://github.com/search?q=org%3Aspring-projects+SpringWeb&type=Code and the results for SpringWeb appear 'only' in package-names. due to these facts and my experience, i would say its bad practice to use it –  Apr 06 '18 at 16:34
0

After exploring more, I found that "SpringWeb" is nothing but the default name given to the model attribute of the submitted "form" in some online tutorials. This has been copied over in many other tutorials. Spring gives the name inside the annotation @ModelAttribute("xyz") to the model attribute.

In fact, @ModelAttribute annotation is not really required in many cases. More details regarding usage on this annotation can be found here: @ModelAttribute annotation, when to use it?

Stacky
  • 503
  • 2
  • 6
  • 23