【问题标题】:Selection variable expressions fields are not working in Thymeleaf选择变量表达式字段在 Thymeleaf 中不起作用
【发布时间】:2021-08-26 08:35:25
【问题描述】:

我在后端文件中正确定义了所有内容。当我尝试使用 通过使用选择变量在 Thymeleaf 中的后端变量 表示它不起作用。它在每个字段中显示错误:

cannot resolve field name

注册.html

<!doctype html "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-4.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">

<head th:replace="/fragments/head"></head>

<body>

<nav th:replace="/fragments/nav :: nav-front"></nav>

<div class="container-fluid mt-5">
    <div class="row">
        <div th:replace="/fragments/categories"></div>
        <div class="col"></div>
        <div class="col-6">
            <h3 class="display-4">Register</h3>

            <form method="post" th:object="${user}" th:action="@{/register}" >

                <div th:if="${#fields.hasErrors('*')}" class="alert alert-danger">
                    There are errors
                </div>

                <div class="form-group">
                    <label for>Username:</label>
                    <input type="text" class="form-control" th:field="*{username}" placeholder="Username">
                    <span class="error" th:if="${#fields.hasErrors('username')}" th:errors="*{username}"></span>
                </div>

                <div class="form-group">
                    <label for>Password:</label>
                    <input type="password" class="form-control" th:field="*{password}">
                    <span class="error" th:if="${#fields.hasErrors('password')}" th:errors="*{password}"></span>
                </div>

                <div class="form-group">
                    <label for>Confirm Password:</label>
                    <input type="password" class="form-control" th:field="*{confirmPassword}">
                    <span class="error" th:if="${passwordMatchProblem}">Passwords do not match</span>
                </div>

                <div class="form-group">
                    <label for>E-mail:</label>
                    <input type="email" class="form-control" th:field="*{email}" placeholder="E-mail" required>
                    <span class="error" th:if="${#fields.hasErrors('email')}" th:errors="*{email}"></span>
                </div>

                <div class="form-group">
                    <label for>Phone number:</label>
                    <input type="text" class="form-control" th:field="*{phoneNumber}" placeholder="Phone number">
                    <span class="error" th:if="${#fields.hasErrors('phoneNumber')}" th:errors="*{phoneNumber}"></span>
                </div>

                <button class="btn btn-danger mb-5">Register</button>

            </form>

        </div>
        <div class="col"></div>
    </div>
</div>

<div th:replace="/fragments/footer"></div>

</body>

</html>

我在使用选择表达式字段的任何地方都会出错。

【问题讨论】:

  • 小点:在Thymeleaf中有变量表达式:${...},还有选择表达式*{...}。您在问题中指的是这两种类型(“选择表达式字段”)吗?还是只是其中之一?
  • edit您的问题显示您收到的错误的全文。我在示例模板中没有看到 name 字段。

标签: spring-boot thymeleaf


【解决方案1】:

您是否将"user" 添加到控制器中的模型中?
您需要将它添加到您的模型中,以便 Thymeleaf 可以访问它。
addAttribute 方法可以像这样使用:model.addAttribute("user", myUser);

【讨论】:

  • 我在控制器中添加了用户模型,但我无法访问注册页面中的用户元素
  • 请提供完整的错误日志。我在 Thymeleaf 中找不到任何“无法解析字段名称”的日志。
猜你喜欢
  • 2014-02-23
  • 2014-08-03
  • 1970-01-01
  • 2018-04-22
  • 1970-01-01
  • 2013-03-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多