【问题标题】:Spring Thymeleaf Cannot resolve 'global'Spring Thymeleaf 无法解析“全局”
【发布时间】:2018-10-08 16:51:41
【问题描述】:

我正在尝试在 Spring 框架中创建登录注册页面应用程序。

在我的注册表单中,intelij idea 显示无法解析“全局”。

我将 springsecurity4 更改为 springsecurity3 但没有更改任何内容。

这是我的表单元素:

<form class="form-signin" th:action="@{/registration}" th:object="${user}" method="post">
  <input type="text" class="form-control" placeholder="Full Name" th:field="*{fullname}" required autofocus>
  <label style="color: red" th:if="${#fields.hasErrors('fullname')}" th:errors="*{fullname}">Username Error</label>
  <input type="text" class="form-control" placeholder="email" th:field="*{email}" required autofocus>
  <label style="color: red" th:if="${#fields.hasErrors('email')}" th:errors="*{email}">Username Error</label>
  <input type="password" class="form-control" placeholder="Password ..." name="password" required>
  <label style="color: red" th:if="${#fields.hasErrors('password')}" th:errors="*{password}">Password Error</label>
  <input type="submit" class="btn btn-lg btn-default btn-block" value="Sign Up" />
  <td th:if="${#fields.hasGlobalErrors()}" th:errors="*{global}">Global Error</td>
</form>

这是我的 html 元素:

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4">

这是我的控制器方法:

@RequestMapping(value = "/registration", method = RequestMethod.POST)
public ModelAndView createNewUser(@Valid User user, BindingResult bindingResult) {
    ModelAndView modelAndView = new ModelAndView();
    User userExists = userService.findUserByEmail(user.getEmail());
    if (userExists != null) {
        bindingResult
                .rejectValue("email", "error.user",
                        "There is already a user registered with the email provided");
    }
    if (bindingResult.hasErrors()) {
        modelAndView.setViewName("registration");
    } else {
        userService.saveUser(user);
        modelAndView.addObject("successMessage", "User has been registered successfully");
        modelAndView.addObject("user", new User());
        modelAndView.setViewName("registration");

    }
    return modelAndView;
}

这是我的 pom.xml 文件:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>

    <dependency>
        <groupId>org.thymeleaf.extras</groupId>
        <artifactId>thymeleaf-extras-springsecurity4</artifactId>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>net.sourceforge.nekohtml</groupId>
        <artifactId>nekohtml</artifactId>
    </dependency>
</dependencies>

【问题讨论】:

  • 为什么要降级springsecurity?
  • @holmis83 我想尽一切办法解决问题。

标签: spring intellij-idea spring-security thymeleaf


【解决方案1】:

我认为问题的根源与th:object 标签有关,并且已经在此处找到解决方案(实际上与其他相关问题相关联):Spring Thyme Leaf checking for global errors results in NPE

此外,我注意到您在 &lt;table&gt; 元素之外使用了 &lt;td&gt; 标记,这不是它应该使用的方式:https://developer.mozilla.org/en-US/docs/Web/HTML/Element/td

【讨论】:

    猜你喜欢
    • 2019-06-11
    • 2019-03-01
    • 2019-02-20
    • 1970-01-01
    • 1970-01-01
    • 2016-12-07
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    相关资源
    最近更新 更多