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