4

我在使用 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>

这是我的控制器:

/** 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 + "]";
    }
}

问题是在控制器的这一点登录为空:

String login = searchForm.getSearchQuery();

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

4

3 回答 3

3

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

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

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

于 2013-01-27T12:19:28.850 回答
0

请将 th:value=" {searchQuery}" 更改为 th:field=" {searchQuery}"。

我希望它会奏效。

于 2014-01-07T07:29:28.307 回答
0

它对我有用:

FormTestController.java

@Controller
public class FormTestController {

    @RequestMapping(value = "/form-test-1.jhtml", method = RequestMethod.GET)
    public String formTest1(@ModelAttribute("form1") Form1TestVO form1TestVO, Model model){
        System.out.println("You've submited: " + form1TestVO.getName())
        model.addAttribute("form1", new Form1TestVO("Form 1 test"));
        return "form-test-1";
    }

}

form-test-1.html

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:layout="http://www.thymeleaf.org" >
<head>
    <title>Form test 1</title>
</head>
<body >

    <form th:object="${form1}" th:action="@{/form-test-1.jhtml}" >
        <input  th:field="*{name}" />
        <button>Send</button>
    </form>

</body>
</html>

Form1TestVO

public class Form1TestVO {
    private String name;

    public Form1TestVO() {
    }

    public Form1TestVO(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

参考

于 2016-12-20T04:36:38.820 回答