ModelAndView.addObject vs Model.addAttribute

2020-05-24 06:21发布

Good day, I'm learning Spring MVC and I'm writting my tiny webapp following this tutorial but I slightly modified it as a "list of tasks" and not "list of users". One thing is not clear to me so I'd like to ask for an explanation. This is my edit.jsp:

<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>  
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>  
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<html>
<head>
    <title>Edit task</title>
</head>
<body>
    <h1>Edit task</h1>
    <form:form method="post" action="/update" modelAttribute="task">
        <table>
            <tr>
                <td>Title</td>
                <td><form:input path="title"/></td>
            </tr>
            <tr>
                <td>Description</td>
                <td><form:textarea path="description"/></td>
            </tr>
            <tr>
                <td></td>
                <td><input type="submit" value="Save"/></td>
            </tr>
        </table>
    </form:form>
</body>
</html>

And this is method editTask in my HomePageController.java (version 1):

 @RequestMapping("/edit")  
 public ModelAndView editTask(@RequestParam String id, @ModelAttribute Task task) {  
   // Retrieve task from the database
     task = taskService.getTask(id);
     ModelAndView model = new ModelAndView("edit");
     model.addObject("task", task);
     return model;
 }

If I click on edit link to edit an item (e.g. /TaskBook/edit?id=1) a form appears but it is not populated. So I changed the method this way (version 2):

 @RequestMapping("/edit")  
 public String editTask(@RequestParam String id, Model model) {
     Task task = taskService.getTask(id);
     model.addAttribute("task", task);
     return "edit";
 }

Now the form is populated when I edit a task. I don't understand what's the difference between model.addObject("task", task) in version 1 and model.addAttribute("task", task) in version 2. Is the task object stored somewhere else or is it lost? Please explain. I use Spring Framework 3.2.1.

Thank you in advance. Vojtech.

EDIT: If I change editTask() to code below it works - the form is populated. But what if some task data were submited, how can I read them now?

@RequestMapping("/edit")  
public ModelAndView editTask(@RequestParam String id) {  
  task = taskService.getTask(id);
  ModelAndView model = new ModelAndView("edit");
  model.addObject("task", task);
  return model;
}

标签: spring-mvc
1条回答
家丑人穷心不美
2楼-- · 2020-05-24 07:22

You've hit an edge case that doesn't occur very often. Let's go by try

@RequestMapping("/edit")  
public String editTask(@RequestParam String id, Model model) {
    Task task = taskService.getTask(id);
    model.addAttribute("task", task);
    return "edit";
}

In this case, Spring will create a Model object from its ModelAndViewContainer and pass that as an argument to your method. So if you had model attributes added earlier, they would be available to you here and the ones you add in will be available later. You return a String view name. Spring will use that String with a ViewResolver to resolve which jsp or other type of view to render or forward to.

With this

@RequestMapping("/edit")  
public ModelAndView editTask(@RequestParam String id, @ModelAttribute Task task) {  
    // Retrieve task from the database
    task = taskService.getTask(id);
    ModelAndView model = new ModelAndView("edit");
    model.addObject("task", task);
    return model;
}

Spring, because of the @ModelAttribute, will create a Task object and pass that as an argument when invoking (reflection) your method. The ModelAndView object you create, add to, and return will be merged with the ModelAndView object contained in the ModelAndViewContainer that is managed by Spring for your request. So things you add here will also be available later.

The hitch: It appears ModelAttribute has priority on the model attributes, so it doesn't get overwritten by the model attributes you add to the ModelAndView object. Actually it gets written to your ModelAndView object, overwriting your "task" attribute. Remember that if you don't specify a value attribute to @ModelAttribute annotation, it uses the type of the argument to give it a name. For example, Task ==> "task", List<Task ==> taskList, etc.

查看更多
登录 后发表回答