2016-04-13 13 views
1

Ich habe ein Projekt Spring Boot mit Thymeleaf als Front-End, die ich von einer Form in Sicht ein Objekt erstellen muß, um zu senden, dieses Objekt ist komplex:thymeleaf Frühling, wie komplexes Objekt Controller

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

Wie Sie siehe die Bean Tasks hat einen Attributnamen Employee. Jetzt möchte ich ein Formular erstellen "Task" So in Controller ich die Bohne Tasks als leere Bohne und Bohne Employee wie diese passieren zu erstellen:

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

} 

Dann: Im Hinblick 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> 

Schließlich implementiere ich eine Steuerung dieses Formular abfangen:

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

Ich habe diesen Fehler, wenn Sie das Formular absenden:

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 

Antwort

0

Der Fehler, sagte Sie, dass das Objekt nicht aufgrund Validierungsfehler gespeichert werden können. Es hat auch gesagt, dass Sie 2 Validierungsfehler haben.
In Ihrem Formular th:object benannt task, so benennen Sie es in Senden Methode. Außerdem müssen Sie BindingResult nach Bean hinzufügen, um Fehler zu behandeln:

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