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中指定了模型就不需要在指定单独的映射参数。