thymeleaf Spring如何将复杂对象发送到控制器

时间:2016-04-13 23:13:47

标签: java spring spring-mvc spring-boot thymeleaf

我有一个项目Spring BootThymeleaf作为前端,我需要从视图中的表单创建一个对象,这个对象很复杂:

@Entity
@Table(name = "tasks", catalog = "explorerrh")
public class Tasks implements java.io.Serializable {

/**
 * 
 */
private static final long serialVersionUID = 1L;
private Integer idtasks;
private Employee employee;
private String taskName;
private String taskDescrption;
private Date taskTime;
private String statut;

如您所见,bean Tasks的属性名称为Employee。 现在我要创建一个表单来创建"Task" 所以在控制器中我将bean Tasks作为空bean和bean Employee传递给这样:

   @RequestMapping(value = "/dashboard.html", method = RequestMethod.POST)
public ModelAndView  loginUser(@ModelAttribute Login login, Model model) {
    Employee employee = employeeService.
            getEmployeeByMail(login.getMailAddress());
        ModelAndView mav = new ModelAndView("dashboard");
        mav.addObject("employee", employee);
        mav.addObject("date", mediumDateFormat.format(date));
        mav.addObject("task", new Tasks());
        return mav;

}

然后: 在视图中Thymeleaf

<form role="form" action="/addTask" th:action="@{/addTask}"  th:object="${task}" method="POST">
                <div class="form-group">
                    <label for="taskTitle"><span class="glyphicon glyphicon-user"></span> Task Title</label>
                    <input type = "text" class = "form-control" th:field="*{taskName}" id = "taskTitle" placeholder = "Enter Task title"/>
                </div>
                <div class="form-group">
                    <label for="taskTitle"><span class="glyphicon glyphicon-user"></span>employee</label>
                    <input type="text" class="form-control"
                           th:field="*{employee}" th:value="${employee}" id="employeeTask"/>
                </div>
                <div class="form-group">
                    <label for="taskDescription">
                        <span class="glyphicon glyphicon-eye-open"></span> Task Description</label>
                    <textarea name="taskDescription" th:field="*{taskDescrption}" class="form-control" rows="5" id="taskDescription"></textarea>
                </div>
                <div class="well">
                    <div id="datetimepicker1" class="input-append date">
                        <input data-format="dd/MM/yyyy hh:mm:ss"
                               name="taskDateTime" th:field="*{taskTime}" type="text"/>
                        <span class="add-on">
                          <i data-time-icon="icon-time" data-date-icon="icon-calendar">
                          </i>
                        </span>
                    </div>
                </div>
                <div class="form-group">
                    <label for="taskStatut"><span class="glyphicon glyphicon-user"></span> Task statut</label>
                    <input type = "text" class = "form-control" th:field="*{taskStatut}" id = "taskStatut" placeholder = "Enter Task statut"/>
                </div>
                <button type="submit" class="btn btn-success"><span class="glyphicon glyphicon-off"></span> add task</button>
            </form>

最后,我实现了一个控制器来拦截这个表单:

   public ModelAndView addTask(@ModelAttribute Tasks tasks) {
    ModelAndView mav = new ModelAndView("dashboard");
    Employee employee = employeeService.
            getEmployeeByIdemployee(String.valueOf(
                    tasks.getEmployee().getIdemployee()));
    tasks.setStatut("Open");
    tasks.setEmployee(employee);
    tasksService.addTask(tasks);
    return mav;
}

发送表单时出现此错误:

Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.

Wed Apr 13 23:47:19 GMT+01:00 2016
There was an unexpected error (type=Bad Request, status=400).
Validation failed for object='tasks'. Error count: 2

1 个答案:

答案 0 :(得分:0)

错误说您由于验证错误而无法保存对象。它还说你有2个验证错误 在名为th:object的{​​{1}}表单中,请在提交方法中重命名。此外,您需要在bean之后添加task来处理错误:

BindingResult