Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java thymeleaf Spring如何将复杂对象发送到控制器_Java_Spring_Spring Mvc_Spring Boot_Thymeleaf - Fatal编程技术网

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

Java thymeleaf Spring如何将复杂对象发送到控制器,java,spring,spring-mvc,spring-boot,thymeleaf,Java,Spring,Spring Mvc,Spring Boot,Thymeleaf,我有一个项目Spring Boot,前端是Thymeleaf,我需要从视图中的表单创建一个对象,这个对象很复杂: @Entity @Table(name = "tasks", catalog = "explorerrh") public class Tasks implements java.io.Serializable { /** * */ private static final long serialVersionUID = 1L; private Integer idtasks

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

@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
。 现在我想创建一个表单来创建一个
“任务”
因此,在控制器中,我将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>
我在发送表单时出现以下错误:

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

错误告诉您,由于验证错误,无法保存对象。它还说您有两个验证错误。
在您的表单
th:object
中命名为
task
,因此在submit方法中重命名它。您还需要在bean之后添加
BindingResult
,以处理错误:

public ModelAndView addTask(@ModelAttribute("task") Tasks tasks, BindingResult bindingResult) {
    if (bindingResult.hasErrors()) {
        //errors handling
    }
}

错误告诉您,由于验证错误,无法保存对象。它还说您有两个验证错误。
在您的表单
th:object
中命名为
task
,因此在submit方法中重命名它。您还需要在bean之后添加
BindingResult
,以处理错误:

public ModelAndView addTask(@ModelAttribute("task") Tasks tasks, BindingResult bindingResult) {
    if (bindingResult.hasErrors()) {
        //errors handling
    }
}