Spring MVC:处理时间表中的值-多个对象

Spring MVC:处理时间表中的值-多个对象,第1张

Spring MVC:处理时间表中的值-多个对象

这对我有用,希望对您有所帮助!

控制者

@Controllerpublic 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.bitsandbytepre.com/ftp/spring-mvc-multi-record-
form.zip



欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5476218.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-12
下一篇 2022-12-12

发表评论

登录后才能评论

评论列表(0条)

保存