1

I have a controller like below

@RequestMapping("/getList")
    public ModelAndView getNetAllList(@ModelAttribute MYObject myObj)  {

//my code here

}

I call this controller via form submission as

<form name="myForm"  th:action="@{/getList}" th:object="${myObjVO}" method="post"

Now my question is, Even I remove @ModelAttribute annotations, this code is working fine.

Does it mean @ModelAttribute is no more necessary with new spring. If not what feature of spring is taking care of mapping between form and model. I am using spring boot 2.0.0 with thymeleaf.

6 Answers6

0

The @ModelAttribute annotation can be used at method and at argument level. At method level, it can be used to configure model attributes before any @RequestMapping method is used. At argument level, it can be used to filter specific properties from a form (e.g. an Employee element that should be constructed from a set of form attributes). See this tutorial for more information on how to use it.

thomi
  • 1,603
  • 1
  • 24
  • 31
  • Yes, and I think my answer implies that you do not need to use it, but if you choose to, then that's what it does. Sorry if I couldn't help. – thomi Apr 10 '18 at 07:38
  • I know what it does. My question is, is it optional now ?? –  Apr 11 '18 at 21:33
0
@ModelAttribute

refers to a property of the Model object (the M in MVC ) so let's say we have a form with a form backing object that is called "Person" Then you can have Spring MVC supply this object to a Controller method by using the @ModelAttribute annotation:

public String processForm(@ModelAttribute("person") Person person){
    person.getStuff();
}

On the other hand the annotation is used to define objects which should be part of a Model. So if you want to have a Person object referenced in the Model you can use the following method:

@ModelAttribute("person")
public Person getPerson(){
    return new Person();
}

This annotated method will allow access to the Person object in your View, since it gets automatically added to the Models by Spring.

Navnath Adsul
  • 364
  • 3
  • 10
0

As a Method Argument

When used as a method argument, it indicates the argument should be retrieved from the model. When not present, it should be first instantiated and then added to the model and once present in the model, the arguments fields should be populated from all request parameters that have matching names.

In the code snippet that follows the employee model attribute is populated with data from a form submitted to the addEmployee endpoint. Spring MVC does this behind the scenes before invoking the submit method:

@RequestMapping(value = "/addEmployee", method = RequestMethod.POST)
public String submit(@ModelAttribute("employee") Employee employee) {
    // Code that uses the employee object

    return "employeeView";
}

Later on in this article we will see a complete example of how to use the employee object to populate the employeeView template.

So, it binds the form data with a bean. The controller annotated with @RequestMapping

can have custom class argument(s) annotated with @ModelAttribute.

This is what is commonly known as data binding in Spring-MVC, a common mechanism that saves you from having to parse each form field individually.

In general, Spring-MVC will always make a call first to that method, before it calls any request handler methods. That is, @ModelAttribute methods are invoked before the controller methods annotated with @RequestMapping are invoked. The logic behind the sequence is that, the model object has to be created before any processing starts inside the controller methods.

0gam
  • 1,343
  • 1
  • 9
  • 21
  • My question is, "is it not necessary anymore to use this annotation". Cause my code is working without using this annotation. –  Apr 11 '18 at 21:35
  • This is what is commonly known as data binding in Spring-MVC, a common mechanism that saves you from having to parse each form field individually. In general, Spring-MVC will always make to @ModelAttribute. this is customer annotation. this is my answer. you need to read content. – 0gam Apr 12 '18 at 00:49
0

First of all, I don't think your code is working. It would only work if you choose either of following solutions.

solution 1.

@RequestMapping("/getList")
    public ModelAndView getNetAllList(@ModelAttribute("myObjVO") MYObject myObj)  {

//my code here

}

<form name="myForm"  th:action="@{/getList}" th:object="${myObjVO}" method="post">

solution 2:

@RequestMapping("/getList")
    public ModelAndView getNetAllList( MYObject myObj)  {

//my code here

}

<form name="myForm"  th:action="@{/getList}" th:object="${myObject}" method="post">

If you closely look these solutions, you will have an idea when @ModelAttribute is required.

So now back to your question

You don't need @ModelAttribute just to use a bean as a parameter. ModelAttribute is used for various other purposes. Read this document and also check following two from stackoverflow answer .

What is @ModelAttribute in Spring MVC?

@ModelAttribute annotation, when to use it?

want2learn
  • 2,471
  • 2
  • 20
  • 37
-1

The @ModelAttribute is used to map object with the model, normally used for initializing a form with default values.

In this case, the name of the attribute in the view ("myObjVO") does not match with the attribute created in the model ("myObj"). Therefore, when the method is called, the myObjVO attribute is sent and replaces the myObj.

In summary, it is not necessary to use @ModelAttribute because the form is initiated in the view and it does not map with the one that is created in the model.

iagooa
  • 42
  • 3
  • you think your answer is right. I wish I can down vote your answer. –  Apr 06 '18 at 14:56
-1

I think this answers your question.

... Also adds a fall-back strategy to instantiate the model attribute from a URI template variable or from a request parameter if the name matches the model attribute name and there is an appropriate type conversion strategy.