这对我有效,希望这有助于!
控制器
@Controller
public class ExampleController {
private Timesheet timesheet;
public ExampleController() {
List<Task> tasks = new ArrayList<Task>();
tasks.add(new Task(1, "Hello", 0, 0, 0, 0, 0, 0, 0));
tasks.add(new Task(2, "Bai", 0, 0, 0, 0, 0, 0, 0));
tasks.add(new Task(3, "I'm back", 0, 0, 0, 0, 0, 0, 0));
this.timesheet = new Timesheet(tasks);
}
@RequestMapping(value = "/timesheet.do", method = RequestMethod.GET)
public ModelAndView displayTimeSheet() {
return new ModelAndView("timesheet", "timesheet", timesheet);
}
@RequestMapping(value = "/timesheet.do", method = RequestMethod.POST)
public ModelAndView updateTimeSheet(@ModelAttribute("timesheet") Timesheet timesheet) {
this.timesheet = timesheet;
return new ModelAndView("timesheet", "timesheet", timesheet);
}
}
JSP页面
<form:form commandName="timesheet">
<table>
<tr>
<th>TaskID</th>
<th>Taskname</th>
<th>D1</th>
<th>D2</th>
<th>D3</th>
<th>D4</th>
<th>D5</th>
<th>D6</th>
<th>D7</th>
</tr>
<c:forEach items="${timesheet.tasks}" var="task" varStatus="tasksRow">
<tr>
<td>
<form:hidden path="tasks[${tasksRow.index}].id"/>
<c:out value="${timesheet.tasks[tasksRow.index].id}"/>
</td>
<td>
<form:hidden path="tasks[${tasksRow.index}].name"/>
<c:out value="${timesheet.tasks[tasksRow.index].name}"/>
</td>
<td>
<form:input path="tasks[${tasksRow.index}].day1hours"/>
</td>
<td>
<form:input path="tasks[${tasksRow.index}].day2hours"/>
</td>
<td>
<form:input path="tasks[${tasksRow.index}].day3hours"/>
</td>
<td>
<form:input path="tasks[${tasksRow.index}].day4hours"/>
</td>
<td>
<form:input path="tasks[${tasksRow.index}].day5hours"/>
</td>
<td>
<form:input path="tasks[${tasksRow.index}].day6hours"/>
</td>
<td>
<form:input path="tasks[${tasksRow.index}].day7hours"/>
</td>
</tr>
</c:forEach>
</table>
<input type="submit"/>
</form:form>
模型类
public class Timesheet {
private List<Task> tasks;
public class Task {
private int id;
private String name;
private int day1hours;
private int day2hours;
private int day3hours;
private int day4hours;
private int day5hours;
private int day6hours;
private int day7hours;
您也可以下载整个示例项目在这里:
http://www.bitsandbytecode.com/ftp/spring-mvc-multi-record-form.zip
非常感谢。它工作正常。现在我的控制器出现了一些问题,但主要问题已得到解决。如果我真的收到这些信息,“检查”的最佳方法是什么?我将不得不遍历时间表/ TaskList。任何建议?尽管如此,非常感谢。欣赏它。 – Geoffrey 2011-03-23 14:01:47
我得到的错误: [23/03/11 15:06:02:099 CET] 00000026 WebApp E [Servlet错误] - [actiontracker]:org.springframework.beans.NullValueInNestedPathException:无效属性'tasks [0] 'bean类[be.pv.actiontracker.domain.TaskList]:无法访问索引属性路径'tasks [0]'中引用的属性的索引值:返回null – Geoffrey 2011-03-23 14:07:24
我添加了一个链接到上面的整个项目,以便您可以下载它看看是否有什么不同,你在做什么。当我自己尝试这个时,我在控制器中没有遇到任何问题。至于你得到的错误,似乎任务集合为空,或者索引为0的元素为空。 – Nate 2011-03-23 14:44:15