1

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

我有一个实体用户类:

@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类的片段:

@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";
    }

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

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

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

编辑: 这是我的pom.xmlweb.xml文件。

4

1 回答 1

0

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

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

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

于 2016-07-02T13:33:15.683 回答