2017/11/27更新
关于SpringMVC接收复杂集合参数的内容:
找了这样一篇博客:
http://jxd-zxf.iteye.com/blog/2072300/
需要注意的是,ajax提交参数的时候,需要用JSON.Stringfy进行一下转化,否则就没有办法提交成功。
Controller默认支持的参数类型
处理器形参中添加如下类型的参数处理适配器会默认识别并进行赋值。
- HttpServletRequest
通过request对象获取请求信息 - HttpServletResponse
通过response处理响应信息 - HttpSession
通过session对象得到session中存放的对象 - Model/ModelMap
ModelMap是Model接口的实现类,通过Model或ModelMap向页面传递数据,如下
//调用service查询商品信息
Items item = itemService.findItemById(id);
model.addAttribute("item", item);
// 页面通过${item.XXXX}获取item对象的属性值。
使用Model和ModelMap的效果一样,如果直接使用Model,springmvc会实例化ModelMap。如果使用Model则可以不使用ModelAndView对象,Model对象可以向页面传递数据,View对象则可以使用String返回值替代。
不管是Model还是ModelAndView,其本质都是使用Request对象向jsp传递数据。下面两段代码含义相同。
代码1:
@RequestMapping("/itemEdit")
publicModelAndView itemEdit(HttpServletRequest request) {
//从Request中取id
String strId = request.getParameter("id");
Integer id = null;
//如果id有值则转换成int类型
if (strId != null&& !"".equals(strId)) {
id = newInteger(strId);
} else {
//出错
returnnull;
}
Items items = itemService.getItemById(id);
//创建ModelAndView
ModelAndView modelAndView = new ModelAndView();
//向jsp传递数据
modelAndView.addObject("item", items);
//设置跳转的jsp页面
modelAndView.setViewName("editItem");
returnmodelAndView;
}
代码2:
@RequestMapping("/itemEdit")
publicString itemEdit(HttpServletRequest request, Model model) {
//从Request中取id
String strId = request.getParameter("id");
Integer id = null;
//如果id有值则转换成int类型
if (strId != null&& !"".equals(strId)) {
id = newInteger(strId);
} else {
//出错
returnnull;
}
Items items = itemService.getItemById(id);
//创建ModelAndView
//ModelAndView modelAndView = new ModelAndView();
//向jsp传递数据
//modelAndView.addObject("item", items);
model.addAttribute("item", items);
//设置跳转的jsp页面
//modelAndView.setViewName("editItem");
//return modelAndView;
return"editItem";
}
绑定简单类型
当请求的参数名称和处理器形参名称一致时会将请求参数与形参进行绑定。从Request取参数的方法可以进一步简化。
1,支持的数据类型
参数类型推荐使用包装数据类型,因为基础数据类型不可以为null
- 整形:Integer、int
- 字符串:String
- 单精度:Float、float
- 双精度:Double、double
- 布尔型:Boolean、boolean
说明:对于布尔类型的参数,请求的参数值为true或false。
处理器方法:
public String editItem(Model model,Integer id,Boolean status) throws Exception
请求url:
http://localhost:8080/xxx.action?id=2&status=false
2,@RequestParam
- value:参数名字,即入参的请求参数名字,如value=“item_id”表示请求的参数区中的名字为item_id的参数的值将传入;
- required:是否必须,默认是true,表示请求中一定要有相应的参数,否则将报:
TTP Status 400 - Required Integer parameter ‘XXXX’ is not present - defaultValue:默认值,表示如果请求中没有同名参数时的默认值
定义如下:
public String editItem(@RequestParam(value="item_id",required=true) String id) {
}
形参名称为id,但是这里使用value=”item_id”限定请求的参数名为item_id,所以页面传递参数的名必须为item_id。
注意:如果请求参数中没有item_id将抛出异常:
HTTP Status 500 - Required Integer parameter ‘item_id’ is not present
这里通过required=true限定item_id参数为必需传递,如果不传递则报400错误,可以使用defaultvalue设置默认值,即使required=true也可以不传item_id参数值
使用pojo接收表单数据
1,普通pojo接收表单
如果提交的参数很多,或者提交的表单中的内容很多的时候可以使用pojo接收数据。要求pojo对象中的属性名和表单中input的name属性一致。
页面定义如下:
<input type="text" name="name"/>
<input type="text" name="price"/>
pojo定义如下:
private String name;
private float price;
请求的参数名称和pojo的属性名称一致,会自动将请求参数赋值给pojo的属性。
@RequestMapping("/updateitem")
public String updateItem(Items items) {
itemService.updateItem(items);
return"success";
}
注意:提交的表单中不要有日期类型的数据,否则会报400错误。如果想提交日期类型的数据需要用到后面的自定义参数绑定的内容。
2,绑定包装pojo
包装对象定义如下:
Public class QueryVo {
private Items items;
}
页面定义如下:
<input type="text" name="items.name" />
<input type="text" name="items.price" />
Controller方法定义如下
@RequestMapping("/queryitem")
public String queryItem(QueryVo queryVo) {
System.out.println(queryVo.getItems().getName());
System.out.println(queryVo.getItems().getPrice());
return null;
}
自定义参数绑定
由于日期数据有很多种格式,所以springmvc没办法把字符串转换成日期类型。所以需要自定义参数绑定。前端控制器接收到请求后,找到注解形式的处理器适配器,对RequestMapping标记的方法进行适配,并对方法中的形参进行参数绑定。在springmvc这可以在处理器适配器上自定义Converter进行参数绑定。如果使用mvc:annotation-driven可以在此标签上进行扩展
1,自定义Converter
Public class DateConverter implements Converter<String, Date> {
@Override
public Date convert(String source) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
returnsimpleDateFormat.parse(source);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
}
2,配置Converter
<!-- 加载注解驱动 -->
<mvc:annotation-drivenconversion-service="conversionService"/>
<!-- 转换器配置 -->
<beanid="conversionService"
class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<propertyname="converters">
<set>
<beanclass="cn.itcast.springmvc.convert.DateConverter"/>
</set>
</property>
</bean>
高级参数绑定
1,绑定数组
例如我们需要此的功能如下:
功能要求商品列表页面中的每个商品前有一个checkbook,选中多个商品后点击删除按钮把商品id传递给Controller,根据商品id删除商品信息
1.1,Jsp中实现
<c:forEach items="${itemList }" var="item">
<tr>
<td><input name="ids" value="${item.id}" type="checkbox"></td>
<td>${item.name }</td>
<td>${item.price }</td>
<td><fmt:formatDate value="${item.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/></td>
<td>${item.detail }</td>
<td><a href="${pageContext.request.contextPath }/itemEdit.action?id=${item.id}">修改</a></td>
</tr>
</c:forEach>
页面中多个checkbox向controller中传递id。
<table width="100%" border=1>
<tr>
<td>商品名称</td>
<td>商品价格</td>
<td>生产日期</td>
<td>商品描述</td>
<td>操作</td>
</tr>
<tr>
<td><input name="ids" value="1" type="checkbox"></td>
<td>台式机</td>
<td>3000.0</td>
<td>2016-02-03 13:22:53</td>
<td></td>
<td><a href="/springmvc-web/itemEdit.action?id=1">修改</a></td>
</tr>
<tr>
<td><input name="ids" value="2" type="checkbox"></td>
<td>笔记本</td>
<td>6000.0</td>
<td>2015-02-09 13:22:57</td>
<td></td>
<td><a href="/springmvc-web/itemEdit.action?id=2">修改</a></td>
</tr>
<tr>
<td><input name="ids" value="3" type="checkbox"></td>
<td>背包</td>
<td>200.0</td>
<td>2015-02-06 13:23:02</td>
<td></td>
<td><a href="/springmvc-web/itemEdit.action?id=3">修改</a></td>
</tr>
</table>
1.2,Controller中实现
Controller方法中可以用String[]接收,或者pojo的String[]属性接收。两种方式任选其一即可。
@RequestMapping("/queryitem")
public String queryItem(QueryVo queryVo, String[] ids) {
System.out.println(queryVo.getItems().getName());
System.out.println(queryVo.getItems().getPrice());
System.out.println(ids.toString());
return null;
}
或者在pojo中直接定义一个ids属性即可。
2,将表单的数据绑定到List
例如我们的需求如下:
要想实现商品数据的批量修改,需要在商品列表中可以对商品信息进行修改,并且可以批量提交修改后的商品数据。
2.1,接收商品列表的pojo
List中存放对象,并将定义的List放在包装类中,使用包装pojo对象接收。
2.2,Jsp改造
<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>
Name属性必须是包装pojo的list属性+下标+元素属性。Jsp做如下改造:
<c:forEach items="${itemList }" var="item">
<tr>
<td><input name="ids" value="${item.id}" type="checkbox"></td>
<td>
<input name="id" value="${item.id}" type="hidden">
<input name="name" value="${item.name }" type="text">
</td>
<td><input name="name" value="${item.price }" type="text"></td>
<td><input name="name" value="<fmt:formatDate value="${item.createtime}" pattern="yyyy-MM-dd HH:mm:ss"/>" type="text"></td>
<td><input name="name" value="${item.detail }" type="text"></td>
<td><a href="${pageContext.request.contextPath }/itemEdit.action?id=${item.id}">修改</a></td>
</tr>
</c:forEach>
备注:varStatus属性常用参数总结下:
-
status.index输出行号,从0开始。−
{status.count} 输出行号,从1开始。
-
status.current当前这次迭代的(集合中的)项−
{status.first} 判断当前项是否为集合中的第一项,返回值为true或false
- ${status.last} 判断当前项是否为集合中的最后一项,返回值为true或false
- begin、end、step分别表示:起始序号,结束序号,跳跃步伐。
2.3,Controller
注意:接收List类型的数据必须是pojo的属性,方法的形参为List类型无法正确接收到数据。
解决post乱码问题
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>