6

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;
}
4

1 回答 1

14

您遇到了不经常发生的边缘情况。让我们尝试一下

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

在这种情况下,Spring 将从其创建一个Model对象ModelAndViewContainer并将其作为参数传递给您的方法。因此,如果您之前添加了模型属性,它们将在此处可供您使用,而您添加的属性稍后将可用。您返回一个String视图名称。Spring 将使用该 String 和 aViewResolver来解析jsp要渲染或转发到哪个或其他类型的视图。

有了这个

@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@ModelAttribute将创建一个Task对象并在调用(反射)您的方法时将其作为参数传递。ModelAndView您创建、添加和返回的对象将与 Spring 为您的请求管理的对象中ModelAndView包含的对象合并。ModelAndViewContainer因此,您在此处添加的内容也将在稍后提供。

问题:它似乎ModelAttribute优先于模型属性,因此它不会被您添加到ModelAndView对象的模型属性覆盖。实际上被写入你的ModelAndView对象,覆盖你的"task"属性。请记住,如果您没有value为注解指定属性@ModelAttribute,它会使用参数的类型为其命名。例如,Task==> "task"List<Task==>taskList等。

于 2013-10-04T15:25:23.957 回答