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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值