【问题标题】:@ModelAttribute return null thymeleaf@ModelAttribute 返回空百里香叶
【发布时间】:2021-12-28 14:30:12
【问题描述】:

我熟悉 Thymeleaf

我正在尝试从控制器中的 POST 映射中获取数据。 但是当我调用employee.getName() 时,我得到了null 变量。

我在网上查了很多,但找不到解决方案。

我的控制器类

@GetMapping
public String getEmployees(Model model) {
    model.addAttribute("newEmployee", new Employee());
    
    return "employees";
}

@PostMapping
public String addEmployee(@ModelAttribute("newEmployee") Employee employee) {
    System.out.println(employee); // return object
    System.out.println(employee.getFirstName()); // return null

    return "employees";
}

带有 Thymeleaf 的 HTML 表单

<div class="modal-body">
    <form th:action="@{/employees}" th:object="${newEmployee}" method="post">
        <div class="row">
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-form-label">First Name <span class="text-danger">*</span></label>
                    <input th:field="*{firstName}" class="form-control" type="text">
                </div>
            </div>
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="col-form-label">Last Name</label>
                    <input th:field="*{lastName}" class="form-control" type="text">
                </div>
            </div>

【问题讨论】:

  • @mikebrucks:对于调试,请添加 logging request-interceptor 并向我们展示 (a) 日志输出,以及 (b) 您的 Employee 类(包括所有 getter/setter)。
  • 员工类只有 getter 需要有 setter 我想从表单的数据中创建新对象员工
  • 用javascript打开表单是动态的,不知道重要不重要

标签: java spring-mvc thymeleaf


【解决方案1】:

查看教程:

@GetMapping("/all")
public String showAll(Model model) {
    model.addAttribute("employees", employeeService.findAll());
    return "employees/all";
}

@GetMapping("/add")
public String createForm(Model model) {
    model.addAttribute("newEmployee", new Employee()); // the form-data object as blank employee to be filled

    return "employees/form";  // the page containing the form
}

@PostMapping
public String addEmployee(@ModelAttribute("newEmployee") Employee employee) {
    System.out.println(employee); // return object
    System.out.println(employee.getFirstName()); // return null if either not set in HTML-form or not mapped from HTML-form to POST correctly

     // save new employee or add to a repository
    employeeService.save(newEmployee);

    model.addAttribute("employees", employeeService.findAll());
    return "redirect:/employees/all";  // use a redirect
}

客户端-服务器交互:

  1. 首先在浏览器中导航到添加表单数据对象的端点,即GET /employees/add。 然后应该加载表单页面,您将有一个空白对象newEmployee 将所有输入字段绑定到以进行提交。
  2. 通过提交点击提交表单后,会发送POST /employees。服务器/控制器应该在employee 参数中接收作为模型属性的表单数据并可以保存它。
  3. 控制器方法重定向回列表端点(与在浏览器中导航到相同)GET /employees/all
<h1>Create employee</h1>
<form th:object="${newEmployee}"
      th:action="@{/employees}"
      method="post">
    <input type="text" name="firstName" id="firstName" th:field="*{firstName}" />
    <input type="text" name="lastName" id="lastName" th:field="*{lastName}" />
    <button type="submit">Submit new employee</button>
</form>
  • th:object 属性指的是我们在模型中放置Employee 实例的键(本例中为newEmployee)。 th:action 具有 @PostMapping 方法的 URL。最后,method 属性设置为"post",因为我们要使用 HTTP POST 方法。
  • 使用th:field=*{…​},我们可以在 HTML 输入和表单数据对象中的字段之间设置双向绑定

【讨论】:

  • 还是不行
  • 我仍然不清楚他们看到的行为应该归咎于什么以及他们需要改变什么。
  • 模型属性无效,它传递空对象
猜你喜欢
  • 1970-01-01
  • 2016-08-14
  • 2015-11-29
  • 1970-01-01
  • 2015-12-09
  • 2018-07-08
  • 2017-04-05
  • 2021-12-11
相关资源
最近更新 更多