java - Spring MVC @ModelAttribute 不包含提交的表单结果

标签 java spring-mvc thymeleaf

我在使用 Thymeleaf 和 Spring-MVC 处理表单时遇到问题。 这是我的观点:

<html xmlns:th="http://www.thymeleaf.org">
    <head>
    </head>
    <body>
        <div class="container" id="containerFragment" th:fragment="containerFragment">
            <form
                action="#"
                th:action="@{/search}"
                th:object="${searchInfo}"
                method="post" >
                <fieldset id="search-query">
                    <input
                        type="text"
                        name="search"
                        value=""
                        id="search"
                        placeholder="Search for user"
                        required="required"
                        th:value="*{searchQuery}" />
                    <input
                        type="submit"
                        value="Search"
                        name="submit"
                        class="submit"/>
                </fieldset>
            </form>
        </div>
    </body>
</html>

这是我的 Controller :

/** Search form */
@RequestMapping(value = "/search", method = RequestMethod.GET)
public String search(Model model) {
    model.addAttribute("searchInfo", new SearchForm());
    return "search";
}

/** Search form */
@RequestMapping(value = "/search", method = RequestMethod.POST)
public ModelAndView search(BindingResult result,
        @Valid @ModelAttribute("searchInfo") SearchForm searchForm) {

    String login = searchForm.getSearchQuery();
    User user = userService.findUserByLogin(login);

    ModelAndView modelAndView = new ModelAndView();
    modelAndView.setViewName("search-results");
    modelAndView.addObject("user", user);

    return modelAndView;
}

搜索表单是:

public class SearchForm {

    String searchQuery;

    public String getSearchQuery() {
        return searchQuery;
    }

    public void setSearchQuery(String searchQuery) {
        this.searchQuery = searchQuery;
    }

    @Override
    public String toString() {
        return "SearchForm [searchQuery=" + searchQuery + "]";
    }
}

问题是此时 Controller 的登录为空:

String login = searchForm.getSearchQuery();

它看起来像是为 POST 方法创建的新 SearchForm 对象,但实际上已经有一个对象,它是在 GET 步骤创建的,应该包含搜索查询。 我无法理解这样的行为。

最佳答案

Spring 应该将 HTML 表单属性映射到您的模型:SearchForm

Spring MVC 使用请求参数和模型对象属性构建 Accordion ,并在将对象传递到 Controller 方法之前将匹配属性设置到模型对象中。

您将 HTML 属性(并自动请求参数名称)命名为 id="search"。但是 SearchForm 没有这样的属性。相反,它有 searchQuery property .因此,在 Spring MVC 无法将 searchQuery 值设置到您的 SearchForm 之后,它会传递带有 null 属性的模型。

关于java - Spring MVC @ModelAttribute 不包含提交的表单结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14546851/

相关文章:

java - 很少有 JDBC 连接

java - Spring MVC Web 应用程序中未找到 Json Webservice

Spring Security CookieTheftException

java - Thymeleaf 上下文 URL 处理 - 如何将语言代码添加到上下文相关 URL (Spring + Thymeleaf)

java - 为什么 ${!blabla.blibli} 在 JSTL 中返回 true 即使 blabla 不存在?

java - 如何获取 Swing 元素的屏幕位置?

Java + jackson解析错误Unrecognized character escape

oracle - Oracle触发器的HIbernate问题,用于从序列中生成ID

java - 如何访问 Thymeleaf 中的 Map 内的 List?

java - Spring Boot + Thymeleaf : How to show books and their authors in one list on the page