Spring MCV:无法处理数据输入

Spring MCV: Cannot handle data input

我的目标是通过处理用户的登录名和密码来授权用户。我试图重现 this example 但遇到了问题。

我有一个实体用户 class:

@DynamicUpdate
public class EntityUser
{
    String login;
    String password;

    public EntityUser() {}

    public EntityUser
    (
            String login,
            String password
        )
    {   
        this.login = login;
        this.password = password;
    }

    public String getLogin() {
        return login;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

这是我的.jsp文件片段:

<form action="#" th:action="@{/loginCheck}" th:object="${user}" method="post">
            <table border="1" width="30%" cellpadding="3">
                <thead>
                    <tr>
                        <th colspan="2">Login Here</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td>User Name</td>
                        <td><input type="text" th:field="*{login}"/></td>
                    </tr>
                    <tr>
                        <td>Password</td>
                        <td><input type="password" th:field="*{password}"/></td>
                    </tr>
                    <tr>
                        <td><input type="submit" value="Login" /></td>
                        <td><input type="reset" value="Reset" /></td>
                    </tr>
                </tbody>
            </table>
        </form>

这是我的片段 Controller.java class:

@RequestMapping(value = "/loginCheck", method = RequestMethod.GET)
    public String userForm(Model model)
    {
        EntityUser user = new EntityUser();
        user.setLogin("login");
        user.setPassword("password");
        model.addAttribute("user", user);

        System.out.println(user.getLogin());
        System.out.println(user.getPassword());

        return "/loginCheck";
    }

    @RequestMapping(value = "/loginCheck", method = RequestMethod.POST)
    public String processUser(@ModelAttribute(value="user") EntityUser user)
    {

        System.out.println(user.getLogin());
        System.out.println(user.getPassword());

        loginInfo = "Jakarta";
        return "redirect:/controllers";
    }

输入值并按下 "Submit" 按钮后,没有调用 GET 或 POST 方法(控制台中没有任何打印),页面正在移动到 /#

另一方面,当我替换 form action="#"form action="/HelloSpringMVC/loginCheck", POST 方法被调用,但是打印的两个字符串都是 "null"

所以,那里出了什么问题?有人知道吗?

已编辑: 这是我的 pom.xml and web.xml 个文件。

您的 pom.xml 没有 spring-boot-starter-thymeleaf 依赖项,因此 thymeleaf 标签未在您的 html/jsp 模板中处理。在 pom.xml 中添加 thymeleaf starter 依赖项并重建:

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

Thymeleaf 将自动在 src/main/resources/templates 中查找扩展名为 .html 的模板。您可能希望通过 editing/creating src/main/resources/application.properties 自定义默认值并添加 http://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html 中定义的任何 spring.thymeleaf.* 应用程序属性,并根据您自己的需要定制这些属性。