【问题标题】:Spring MVC @ModelAttribute doesn't contain submitted form resultsSpring MVC @ModelAttribute 不包含提交的表单结果
【发布时间】:2013-01-10 21:29:08
【问题描述】:

我在使用 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 步骤创建的,应该包含搜索查询。 我无法理解这种行为。

【问题讨论】:

  • 尝试不映射到 FieldSet 而是映射到 id="search" html 标记。
  • 你能说得更具体些吗?我没有到 FieldSet 或其 id 或 Form 标记的显式映射。映射到 thymeleafs "th:object" 属性的表单对象。即使我完全删除 FieldSet 标记,我仍然有问题。

标签: java spring-mvc thymeleaf


【解决方案1】:

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

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

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

【讨论】:

  • 你是对的,问题解决了。这很愚蠢。我页面上的“名称”属性应该包含一个“searchQuery”值。它应该与名称表单属性相同。
【解决方案2】:

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

我希望它会起作用。

【讨论】:

    【解决方案3】:

    它对我有用:

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

    Reference

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-12
      • 1970-01-01
      • 2020-10-31
      • 1970-01-01
      • 1970-01-01
      • 2016-05-21
      • 2017-07-31
      • 2015-05-07
      相关资源
      最近更新 更多