springmvc 中的data bind机制

一、使用@RequestParam将请求参数绑定至方法参数

使用这种方法,不必要求请求的参数名和形参名保持一致。并且,如果参数使用了该注解,则该参数默认是必须提供的(页面必须保证此值存在),但你也可以把该参数标注为非必须的:只需要将@RequestParam注解的required属性设置为false即可(比如,@RequestParam(value="id", required=false))。

@Controller
@RequestMapping("/pets")
@SessionAttributes("pet")
public class EditPetForm {
    // ...
    @RequestMapping(method = RequestMapping.GET)
    public String setupForm(@RequestParam("petId") int petId, ModelMap model) {
        Pet pet = this.clinic.loadPet(petId);
        model.addAttribute("pet", pet);
        return "petForm";
    }

    // ,..
}

二、Stirng 类型的参数绑定

  1. Http请求传递的数据都是字符串String类型的
  2. 属性编辑器(PropertyEditor)可以将string 类型转换成我们需要的java对象( 通过使用setAsText方法)。
  3. 我们可以自定义PropertyEditor,只要继承PropertyEditorSupport类并重写setAsText方法就可以。 Spring中有很多自定义的属性编辑器,都在spring-beans jar包下的org.springframework.beans.propertyeditors包里。

三、数组参数的绑定

  1. 需求 商品的批量删除,用户可以选择多个商品,批量删除。
  2. 表现层的实现 关键:将页面选择的商品id --> controller方法形参中; 方法形参使用数组( items_id)接收页面的请求的多个商品id.

页面定义:

<script language="JavaScript">
        <%-- 提交表单 --%>
        function deleteItems() {
            document.itemsForm.action = "${pageContext.request.contextPath }/items/deleteItems.action";
            document.itemsForm.submit();
        }
        function queryItems() {
            document.itemsForm.action = "${pageContext.request.contextPath }/items/queryItems.action";
            document.itemsForm.submit();
        }
</script>

<td><input type="button" value="批量删除" onclick="deleteItems()"/></td>

<%--多选框 --%>
<td><input type="checkbox" name="items_id" value="${item.id}"/></td>

controller方法定义:

@RequestMapping("deleteItems")
public String deleteItems(Integer[] items_id) {


      //调用service批量删除商品
      return "success";
}

四、List 参数的绑定

需求:批量提交数据时,将提交的数据绑定到List<pojo> 中 ,比如:成绩录入(批量提交) 本例子需求,批量修改,在页面输入多个商品的信息,将多个商品提交到controller方法中.

表现层:controller方法定义: 1.进入商品修改界面(样式,参考商品列表实现 2. 批量修改提交 3. 既然页面没有list属性.可以自己在pojo中设置一个List实例变量,并提供getter、setter方法 4. 那么,就可以通过包装pojo接收,页面传给List实例变量的数据。页面传来的每个数据都是list的元素(每个属性都用一个对象来包装),并在遍历时以其varStatus的值来标识下标.

页面

<script language="JavaScript">
        <%-- 提交表单 --%>
        function editItemsAll() {
            document.itemsForm.action = "${pageContext.request.contextPath }/items/editItemsAll.action";
            document.itemsForm.submit();
        }
        function editItemsQuery() {
            document.itemsForm.action = "${pageContext.request.contextPath }/items/editItemsQuery.action";
            document.itemsForm.submit();
        }
</script>

<td><input type="button" value="查询" onclick="editItemsQuery()"/></td>
<td><input type="button" value="批量修改提交" onclick="editItemsAll()"/></td>

<!--商品列表-->
<table width="100%" border=1>
      <tr>
          <td>商品名称</td>
          <td>商品价格</td>
          <td>生产日期</td>
          <td>商品描述</td>
          <td>操作</td>
      </tr>
      <c:forEach items="${itemsList }" var="item" varStatus="status">
          <tr>
              <td><input name="itemsCustomList[${status.index}].name" value="${item.name }"/></td>
              <td><input name="itemsCustomList[${status.index}].price" value="${item.price }"/></td>
              <td><input name="itemsCustomList[${status.index}].createtime"
                         value="<fmt:formatDate value="${item.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/>"/></td>
              <td><input name="itemsCustomList[${status.index}].detail" value="${item.detail }"/></td>
          </tr>
      </c:forEach>
</table>

controller方法:

//批量修改显示页面
@RequestMapping("/editItemsQuery")
public String editItemsQuery(Model model, ItemsQueryVo itemsQueryVo) throws Exception {

      List<ItemsCustom> itemsList = itemsService.findItemsList(itemsQueryVo);
      model.addAttribute("itemsList", itemsList);

      return "items/editItemsQuery";
}

//修改提交页面
@RequestMapping("/editItemsAll")
public String editItemsAll(ItemsQueryVo itemsQueryVo) {

      //注入service操作
        
      return "success";
}

五、map 参数的绑定

map 与list类似.

转载于:https://my.oschina.net/lemos/blog/790640

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值