【问题标题】:thymeleaf Spring how to send complex object to controllerthymeleaf Spring 如何将复杂对象发送到控制器
【发布时间】:2016-08-05 07:14:38
【问题描述】:

我有一个项目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

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2014-10-20
      • 2015-09-04
      • 1970-01-01
      • 2020-10-19
      • 2020-09-22
      • 2019-09-29
      • 1970-01-01
      • 2019-05-20
      • 1970-01-01
      相关资源
      最近更新 更多