【问题标题】:Spring MCV: Cannot handle data inputSpring MVC:无法处理数据输入
【发布时间】:2016-11-03 21:58:28
【问题描述】:

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

我有一个 Entity User 类:

@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 文件。

【问题讨论】:

  • 看起来没有使用 thymeleaf 将输入值传递给模型中的用户对象。您正在尝试混合使用视图类型。 JSP 和百里香叶。您可能想坚持其中一个。我建议坚持使用百里香,并以这种方式解决问题。你能包含你的 pom.xml 和 MVC 配置类吗?
  • 添加了Pom.xml和web.aml

标签: spring jsp spring-mvc thymeleaf request-mapping


【解决方案1】:

您的 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/current/reference/html/common-application-properties.html 中定义的任何 spring.thymeleaf.* 应用程序属性来自定义默认值,并根据您自己的需要进行调整。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-01
    • 2018-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多