春天的Thymeleaf无法解决全球性的问题。

时间:2018-04-27 19:38:17

标签: spring intellij-idea spring-security thymeleaf

我正在尝试在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>

1 个答案:

答案 0 :(得分:0)

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

此外,我注意到您在<td>元素之外使用<table>标记,根据https://developer.mozilla.org/en-US/docs/Web/HTML/Element/td

,这不是应该使用的标记