2016-04-13 65 views
1

我有一个项目Spring BootThymeleaf为前端,我需要从视图中窗体创建一个对象,这个对象是复杂的:thymeleaf Spring是如何复杂的对象发送到控制器

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

正如你见豆Tasks有一个属性名称Employee。 现在我想创建一个表单创建"Task" 所以在控制器我通过豆Tasks为空豆及豆制品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 

回答

0

错误说你那个对象不能保存由于验证错误。另外它说你有2个验证错误。
在您的表单th:object中命名为task,所以在提交方法中重命名它。您还需要在bean后面添加BindingResult以处理错误:

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