Spring Mvc 表单数据与Controller中模型(Model)的映射

来源:互联网 发布:电子书下载 知乎 编辑:程序博客网 时间:2024/06/12 20:54

Controller方法中配置的Model中包含了在Controller方法中自定义的参数,此参数在Model中的key等于类名首字母小写。

thymeleafFacmily.html

......<form th:action="@{/thymeleafFamily}" th:object="${family}" method="post" style="border: 1px solid greenyellow"><span th:text="#{family.name}"></span> <input type="text" th:field="*{name}" /><br/>        <span th:text="#{family.totalZiChan}"></span><input type="text" th:field="*{totalZiChan}" /><br/>        <span th:text="#{family.createDate}"></span><input type="text" th:field="*{createDate}" th:errorclass="fielderror_box" />......<input type="submit" id="btnAddID" name="btnAdd" value="ADD" /></form>......

ThymeleafController.java

......@RequestMapping(path = "/thymeleafFamily", params = {"btnAdd"})    public String thymeleafFamilyAdd( ModelMap model, Family familySubmit            ,  BindingResult bindingResult, RedirectAttributes redirectAttributes){        if(bindingResult.hasErrors()){            return "thymeleafFamily";        }        Family family = (Family) model.get("family");        familyService.addFamily(familySubmit);        model.clear();        return "redirect:thymeleafFamily";    }  ......

这里写图片描述
从上图中可以看到前端submit的数据转换成了类Family,映射在model.key=’family’以及contoller对应的参数familySubmit映射,且二者指向的都是同一个实例。

结论

Controller中指定了模型就不需要在指定单独的映射参数。

阅读全文
0 0
原创粉丝点击