Spring MVC:处理来自时间表 - 多个对象的值 [英] Spring MVC: processing values from timesheet - multiple objects

查看:107
本文介绍了Spring MVC:处理来自时间表 - 多个对象的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用时间表/ actiontracker应用程序。我使用Spring MVC。这是我目前的情况:

I'm working on a timesheet/actiontracker application. I'm using Spring MVC. This is my current situation:

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

I got a form with all the tasks. Next to these tasks there are seven textboxes (which display the amount of hours for the next seven days). A user must be able to save the tasks (and the hours). I'm not really sure how to send these results to the controller. It contains a list of objects. Let's say...

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

它应该发送任务对象的列表,所以我可以插入/更新/删除一个timerecord。

It should send a list of task objects back, so I can insert/update/delete a timerecord.

任何想法如何做? >

Any ideas how to do this?

推荐答案

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

This worked for me, hope this helps!

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

}

strong>

JSP Page

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

您可以在这里下载整个示例项目:

You can download the entire example project here:

http://www.bitsandbytecode.com/ftp/ spring-mvc-multi-record-form.zip

这篇关于Spring MVC:处理来自时间表 - 多个对象的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆