Springmvc中参数的绑定

时间:2020-12-31 21:29:47

.处理器适配器在执行Handler之前需要把http请求的key/value数据绑定到Handler方法形参数上。

1.默认支持的参数类型

HttpServletRequest,HttpServletResponse,HttpSession,Model/ModelMap

ModelMap是Model接口的实现类,通过Model或ModelMap向页面传递数据,如下:

//调用service查询商品信息

model.addAttribute("item", item);

页面通过${item.XXXX}获取item对象的属性值。

使用Model和ModelMap的效果一样,如果直接使用Model,springmvc会实例化ModelMap。

2.简单类型

整型,小数,字符串,boolean

浏览器:

url:http://localhost:8080/demo/test.action?id=2&name=zcj&flag=true

Controller:

@Controller
public class ItemController { @RequestMapping("/test")
public String test(HttpServletRequest request,HttpServletResponse response, HttpSession session, Model model,
Integer id,String name,Boolean flag){ }

@RequestMapping(value="/editItems",method={RequestMethod.POST,RequestMethod.GET})
   //@RequestParam里边指定request传入参数名称和形参进行绑定。
  //通过required属性指定参数是否必须要传入,形参名称为id,但是这里使用value=" item_id"限  //定请求的参数名为item_id,所以页面传递参数的名必须为item_id。

  //通过defaultValue可以设置默认值,如果id参数没有传入,将默认值和形参绑定。
 public String editItems(Model model,@RequestParam(value="items_id",required=true) Integer id)throws Exception {

  return "items/editItems";
  }



3.定义日期格式进行参数绑定

public class CustomDateConverter implements Converter<String, Date> {

    @Override
public Date convert(String source) {
try {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return simpleDateFormat.parse(source);
} catch (Exception e) {
e.printStackTrace();
}
return null;
} }

<mvc:annotation-driven conversion-service="conversionService">
</mvc:annotation-driven>
<!-- conversionService -->
<bean id="conversionService"
class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<!-- 转换器 -->
<property name="converters">
<list>
<bean class="cn.itcast.ssm.controller.converter.CustomDateConverter"/>
</list>
</property>
</bean>

3. 字符串数组

页面定义如下:
页面选中多个checkbox向controller方法传递
<input type="checkbox" name="item_id" value="001"/>
<input type="checkbox" name="item_id" value="002"/>
<input type="checkbox" name="item_id" value="002"/> 传递到controller方法中的格式是:001,002,003 Controller方法中可以用String[]接收,定义如下:
public String deleteitem(String[] item_id)throws Exception{
System.out.println(item_id);

4.List

List中存放对象,并将定义的List放在包装类中,action使用包装对象接收。

List中对象:
成绩对象
Public class QueryVo {
Private List<Items> itemList;//商品列表 //get/set方法..
} 包装类中定义List对象,并添加get/set方法如下: 页面定义如下: <tr>
<td>
<input type="text" name=" itemsList[0].id" value="${item.id}"/>
</td>
<td>
<input type="text" name=" itemsList[0].name" value="${item.name }"/>
</td>
<td>
<input type="text" name=" itemsList[0].price" value="${item.price}"/>
</td>
</tr>
<tr>
<td>
<input type="text" name=" itemsList[1].id" value="${item.id}"/>
</td>
<td>
<input type="text" name=" itemsList[1].name" value="${item.name }"/>
</td>
<td>
<input type="text" name=" itemsList[1].price" value="${item.price}"/>
</td>
</tr> 上边的静态代码改为动态jsp代码如下:
<c:forEach items="${itemsList }" 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>
.....
.....
</tr>
</c:forEach> Contrller方法定义如下: public String useraddsubmit(Model model,QueryVo queryVo)throws Exception{
System.out.println(queryVo.getItemList());
}

5.Map
在包装类中定义Map对象,并添加get/set方法,action使用包装对象接收。
包装类中定义Map对象如下:
Public class QueryVo {
private Map<String, Object> itemInfo = new HashMap<String, Object>();
//get/set方法..
} 页面定义如下: <tr>
<td>学生信息:</td>
<td>
姓名:<inputtype="text"name="itemInfo['name']"/>
年龄:<inputtype="text"name="itemInfo['price']"/>
.. .. ..
</td>
</tr> Contrller方法定义如下: public String useraddsubmit(Model model,QueryVo queryVo)throws Exception{
System.out.println(queryVo.getStudentinfo());
}

1.1.1.1  List

List中存放对象,并将定义的List放在包装类中,action使用包装对象接收。

List中对象:

成绩对象

Public class QueryVo {

Private List<Items> itemList;//商品列表

//get/set方法..

}

包装类中定义List对象,并添加get/set方法如下:

页面定义如下:

<tr>

<td>

<input type="text" name=" itemsList[0].id" value="${item.id}"/>

</td>

<td>

<input type="text" name=" itemsList[0].name" value="${item.name }"/>

</td>

<td>

<input type="text" name=" itemsList[0].price" value="${item.price}"/>

</td>

</tr>

<tr>

<td>

<input type="text" name=" itemsList[1].id" value="${item.id}"/>

</td>

<td>

<input type="text" name=" itemsList[1].name" value="${item.name }"/>

</td>

<td>

<input type="text" name=" itemsList[1].price" value="${item.price}"/>

</td>

</tr>

上边的静态代码改为动态jsp代码如下:

<c:forEach items="${itemsList }" 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>

.....

.....

</tr>

</c:forEach>

Contrller方法定义如下:

public String useraddsubmit(Model model,QueryVo queryVo)throws Exception{

System.out.println(queryVo.getItemList());

}