SpringMVC框架(1)之(1.3 参数绑定)

参数绑定

一、绑定简单类型参数:整型、字符串、float/double、日期、布尔(eg:Controller中方法 public String editItemsSubmit(String name,Float price))

(eg:4.2 editItems.jspname=“name /price /detail”;action="${pageContext.request.contextPath}/items/editItemsSubmit.action",提交到 editItemsSubmit;)
法一:public String editItemsSubmit( String name,Float price ) throws Exception;
法二:public String editItemsSubmit( Items items ) throws Exception;

二、绑定简单POJO类型:(eg:Controller中方法 public String editItemsSubmit( ItemsCustom itemsCustom)将一、中简单类型参数换成对应的 pojo类型);

简单POJO即只包含简单类型的属性;
绑定过程:request 请求参数名称和 POJo类属性名一致,可以自动绑定;
① public String editItemsSubmit(Integer id, ItemsCustom itemsCustom ) throws Exception;
② itemsService.updateItems(id, itemsCustom );
【 类 ItemsCustom是类 Items的扩展类,包含 Items类所有属性的 】。

(② 中要传入参数是 itemsCustom,ItemsCustom类是 Item类扩展类(包含 Items类中所有属性);所以方法中传入参数是 ItemsCustom itemsCustom;
4.2 editItems.jspname=“id / name / price / detail”;value="$ {item.id}、$ {item.name}、$ {item.price}、$ {item.detail}" )

三、绑定包装类型:包装类型 ItemsQueryVo 中包含 ItemsCustom itemsCustom类(eg:jsp 页面中 name=“itemsCustom.price”)

① public String editItemsSubmit(Integer id,ItemsCustom itemsCustom, ItemsQueryVo itemsQueryVo ) throws Exception;
② itemsService.updateItems(id, itemsCustom );
【 包装类 ItemsQueryVo包含 ItemsCustom类,所以 jsp页面中 name=被包含的属性.XX即 request 请求参数名称(name=ItemsCustom.XX)和 POJO类属性名(ItemsCustom)一致 】。

4.2 editItems.jspname=“itemsCustom.id / itemsCustom.name / itemsCustom.price /itemsCustom.detail”;value="$ {item.id}、$ {item.name}、$ {item.price}、$ {item.detail}" )

4.2 editItems.jsp

<body>
  <form action="${pageContext.request.contextPath}/items/editItemsSubmit.action" method="post">
  <input type="hidden" name="id、itemsCustom.id" value="${item.id}"/>
  <table>
     <tr><td>商品名称:</td>
         <td><input type="text" name="name、itemsCustom.name" value="${item.name}"></td></tr>
     <tr><td>商品价格:</td>
         <td><input type="text" name="price、itemsCustom.price" value="${item.price}"></td></tr>
     <tr><td>商品描述:</td>
         <td><input type="text" name="detail、itemsCustom.detail" value="${item.detail}"></td></tr>
     <tr><td colspan="2" align="center"><input type="submit" value="提交"/></td></tr>
  </table>
  </form>
</body>

3.1 ItemsController.java

@Controller
@RequestMapping("/items")
public class ItemsController{
	@Autowired
	private ItemsService itemsService;

	/* @RequestMapping("/editItemsSubmit")
	public String editItemsSubmit(Items items) throws Exception{
		return "forward:queryItems.action";
	} */
	@RequestMapping("/editItemsSubmit")
	public String editItemsSubmit(Integer id,ItemsCustom itemsCustom,ItemsQueryVo itemsQueryVo) throws Exception{
		itemsService.updateItems(id,itemsCustom);
		return "forward:queryItems.action";
	}
}

四、数组类型绑定

JSP页面中提交的控件的 name值 要与Controller 方法中参数名 一致(传递参数过来);
需求:用户选择批量删除商品
(点击 4.1 itemsList.jsp 中“批量删除”按钮时,调用< script>中的 deleteItems()方法,方法的 action的 URL(即…/items /deleteItems.action)指向 4.2 ItemsController.java 中 @RequestMapping("/deleteItems")方法; 4.1 itemsList.jsp
<td><input type="checkbox" name="delete_id" value="${item.id}"/> ${item.name}</td> name="delete_id"4.2 ItemsController.javapublic String deleteItems(Integer[] delete_id) 方法中的参数 ( Integer[ ] delete_id ) 保持一致,即从 jsp页面提交后传参到 Controller;

4.1 itemsList.jsp

<%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<head>
    <script type="text/javascript">
       function deleteItems(){
       	  document.itemsForm.action="${pageContext.request.contextPath}/items/deleteItems.action";
       	  document.itemsForm.submit();
       }
    </script>
</head>
<body>
   <form name="itemsForm" action="">
    <table width="100%" border="1">
    <tr><td><input type="button" value="查询"/>
            <input type="button" value="批量删除" onclick="deleteItems()" />
        </td>
	    <td colspan="3">
		    <select>
			    <c:forEach items="${itemsType}" var="itemsType">
			    	<option value="${itemsType.key}">${itemsType.value}</option>
			    </c:forEach>
		    </select>
	    </td></tr>
       <tr><td>商品名称</td><td>商品价格</td><td>订购日期</td><td>商品描述</td><td>操作</td></tr>
       <c:forEach items="${items}" var="item">
          <tr><td><input type="checkbox" name="delete_id" value="${item.id}"/> ${item.name}
              </td>
              <td>${item.price}</td>
              <td><fmt:formatDate value="${item.createtime}" pattern="yyyy-MM-dd"/></td>
              <td>${item.detail}</td>
              <td>
                <a href="${pageContext.request.contextPath}/items/editItems.action?id=${item.id}">修改</a>
              </td>
          </tr>
       </c:forEach>
    </table>
   </form>
</body>

4.2 ItemsController.java

@Controller
@RequestMapping("/items")
public class ItemsController{

	@RequestMapping("/deleteItems")
	public String deleteItems(Integer[] delete_id) throws Exception{
		//删除
		return "success";
	}  
	
}

4.3 success.jsp

<body>
   Success!
</body>

五、List集合类型绑定

Controller类的方法中形参使用包装类,包装类中包含 JSP页面中要使用到的 list集合的属性名;(eg: 包装类中集合itemsList,JSP页面显示的 name值:itemsList [ $ {s.index}].name);(即 5.2 ItemsController.java中方法public String updateItemsList(ItemsQueryVo itemsQueryVo) throws Exception使用包装类 ItemsQueryVo,包装类中包含private List<ItemsCustom> itemsList;的 itemsList集合属性,所以 5.1 editItemsList.jsp可使用name="itemsList[${s.index}].name"进行 itemsList的遍历;)
需求:批量修改商品信息:进入批量修改页面,填写数据,提交;
5.1 editItemsList.jsp 中显示商品时 <td><input type="text" name="" value="${item.name}"></td> 中 name的值是 “itemsList[0].name” ,所以 5.2 ItemsController.java 中方法public String updateItemsList(ItemsQueryVo itemsQueryVo) throws Exception的参数是 ItemsQueryVo 包装类(ItemsQueryVo 类中含有 ItemsCustom属性,再添加 private List<ItemsCustom> itemsList itemsList集合,ItemsCustom类是 Items类的扩展类包含 Items类的所有字段); 5.1 editItemsList.jsp 页面中的< c:forEach>标签添加 varStatus属性,则<input type="text" name="itemsList[${s.index}].name" value="${item.name}"> 中name可以使用 varStatus值的下表进行表示;)
5.1 editItemsList.jsp

<head>
    <script type="text/javascript">
       function updateItems(){
          document.itemsForm.action="${pageContext.request.contextPath}/items/updateItems.action";
          document.itemsForm.submit();
       }
    </script>
</head>
<body>
  <form>
    <table width="100%" border="1">
    <tr><td><td><input type="button" value="查询"/>
            <input type="button" value="批量修改" onclick="updateItems()" />
        </td>
      <td colspan="3">
        <select>
          <c:forEach items="${itemsType}" var="itemsType">
            <option value="${itemsType.key}">${itemsType.value}</option>
          </c:forEach>
        </select>
      </td></tr>
       <tr><td>商品名称</td><td>商品价格</td><td>订购日期</td><td>商品描述</td><td>操作</td></tr>
         items="${items}" var="item" varStatus="s">
          <tr><td><input type="text" name="itemsList[${s.index}].name" value="${item.name}"></td>
              <td><input type="text" name="itemsList[${s.index}].price" value="${item.price}"></td>
              <td><fmt:formatDate value="${item.createtime}" pattern="yyyy-MM-dd"/></td>
              <td>${item.detail}</td>
              <td>
                <a href="${pageContext.request.contextPath}/items/editItems.action?id=${item.id}">修改</a>
              </td>
          </tr>
       </c:forEach>
    </table>
  </form>
</body>

5.2 ItemsController.java

@Controller
@RequestMapping("/items")
public class ItemsController{
	@Autowired
	private ItemsService itemsService;

	@RequestMapping("/editItemsList")
	public ModelAndView editItems() throws Exception{
		List<ItemsCustom> itemList=itemsService.findItemsList(null);
		ModelAndView modelAndView=new ModelAndView();
		modelAndView.addObject("items",itemsList);//jsp页面拿到items
		modelAndView.setViewName("editItemsList");
		return modelAndView;
	}

	//批量修改
	@RequestMapping("/updateItemsList")
	public String updateItemsList(ItemsQueryVo itemsQueryVo) throws Exception{
		return "success";
	}  
}

5.3 ItemsQueryVo .java

public class ItemsQueryVo{
	private ItemsCustom itemsCustom;
	private List<ItemsCustom> itemsList;
	set、get();	
}

5.4 success.jsp

<body>
   Success!
</body>

猜你喜欢

转载自blog.csdn.net/qq_41029923/article/details/84785078