java - Spring MVC : processing values from timesheet - multiple objects

标签 java spring jsp spring-mvc controller

我正在开发一个时间表/actiontracker 应用程序。我正在使用 Spring MVC。这是我的现状:

我得到了一个包含所有任务的表格。在这些任务旁边有七个文本框(显示接下来 7 天的小时数)。用户必须能够保存任务(和时间)。我不太确定如何将这些结果发送到 Controller 。它包含一个对象列表。 比方说...

TaskID - Taskname - D1 - D2 - D3 ...
1        Hello      5    3    2
2        Bai        4    2    1
3        I'm back   3    4    3

它应该发回一个任务对象列表,这样我就可以插入/更新/删除一个时间记录。

有什么办法吗?

最佳答案

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

Controller

@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

关于java - Spring MVC : processing values from timesheet - multiple objects,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5403291/

相关文章:

通过 Java Servlet 转发后,表单重置的 Javascript 不起作用

java - 如何保留用于设置 JsonSubTypes 的属性?

java - JSP 和 CSS 文件未在浏览器 Java WebApp 中加载

java - 从我的应用程序读取 jsp 页面输出

java - 为什么 Spring Data 存储库上的 getOne(...) 不会抛出 EntityNotFoundException?

Java健康检查最佳实践

java - 如何在 Spring 中编写 mongodb 聚合减少查询?

java - Netbeans java 应用程序 - 在 MySql 数据库上执行查询

java - 用 Java 写一个 RSS 阅读器

java - 什么是NullPointerException,我该如何解决?