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;
}
You've hit an edge case that doesn't occur very often. Let's go by try
In this case, Spring will create a
Model
object from itsModelAndViewContainer
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 aString
view name. Spring will use that String with aViewResolver
to resolve whichjsp
or other type of view to render or forward to.With this
Spring, because of the
@ModelAttribute
, will create aTask
object and pass that as an argument when invoking (reflection) your method. TheModelAndView
object you create, add to, and return will be merged with theModelAndView
object contained in theModelAndViewContainer
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 theModelAndView
object. Actually it gets written to yourModelAndView
object, overwriting your"task"
attribute. Remember that if you don't specify avalue
attribute to@ModelAttribute
annotation, it uses the type of the argument to give it a name. For example,Task
==>"task"
,List<Task
==>taskList
, etc.