一.高级参数绑定
1、将前端传来的参数绑定数组/集合中
1)数组直接接收
@RequestMapping(value = "/arrayTest.action")
public void arrayTest(Integer[] ids){
System.out.println(Arrays.toString(ids));
}
2)POJO中的数组接收
@RequestMapping(value = "/arrayTest.action")
public void arrayTest(QueryVo vo){
System.out.println(Arrays.toString(vo.getIds()));
}
3)集合直接接收(报错,无法直接接收)
@RequestMapping(value = "/arrayTest.action")
public void arrayTest(List<Integer> ids){
System.out.println(ids);
}
4)POJO中的集合接收
@RequestMapping(value = "/arrayTest.action")
public void arrayTest(QueryVo vo){
System.out.println(vo.getIds());
}
2、将前端传来的所有参数保存到集合中
JSP页面样式

<form action="${pageContext.request.contextPath }/arrayTest.action" method="post">
商品列表:
<table width="100%" border=1>
<tr>
<td>选择</td>
<td>商品名称</td>
<td>商品价格</td>
<td>生产日期</td>
<td>商品描述</td>
<td>操作</td>
</tr>
<c:forEach items="${itemList }" var="item" varStatus="s">
<tr>
<td><input type="checkbox" name="ids" value="${item.id }"></td>
<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 HH:mm:ss"/></td>
<td>${item.detail }</td>
<td><a href="${pageContext.request.contextPath }/itemEdit.action?id=${item.id}">修改</a></td>
</tr>
</c:forEach>
</table>
<input type="submit"/>
</form>
QueryVO.java
public class QueryVo {
List<Item> itemsList;
public List<Item> getItemsList() {
return itemsList;
}
public void setItemsList(List<Item> itemsList) {
this.itemsList = itemsList;
}
}
测试类
@RequestMapping(value = "/arrayTest.action")
public void arrayTest(QueryVo vo){
System.out.println(vo.getItemsList());
}
二.@RequestMapper
1、URL路由映射
@RequestMapping(value = "/xxx.action")
2、在类上加
// 简化请求路径 /itemList ==> /item/itemList
@RequestMapping("/item")
public class ItemsController {
3、方法限定
限定GET方法:@RequestMapping(method = RequestMethod.GET)
限定POST方法:@RequestMapping(method = RequestMethod.POST)
GET和POST都可以:@RequestMapping(method = {RequestMethod.GET,RequestMethod.POST})
4、多个URL映射到同一方法
@RequestMapping(value = "{/itemList.action,/item123.action}")
三.Controller方法返回值
1、返回ModelAndView
controller方法中定义ModelAndView对象并返回,对象中可添加model数据、指定view。(一般用于异常处理器中)
// 配置请求的url
@RequestMapping(value = "/itemList.action")
public ModelAndView queryItemList(){
// 创建页面需要显示的商品数据
List<Item> items = itemService.selectItemsList();
ModelAndView modelAndView = new ModelAndView();
// 放到request域中
modelAndView.addObject("itemList",items);
//modelAndView.setViewName("/WEB-INF/jsp/itemList.jsp");
modelAndView.setViewName("itemList");
return modelAndView;
}
2、返回String
优点:解耦,返回视图路径,model带数据,官方推荐此种方式。解耦,数据,视图,分离,MVC 。
// 配置请求的url
@RequestMapping(value = "/itemList.action")
public String queryItemList(Model model){
// 创建页面需要显示的商品数据
List<Item> items = itemService.selectItemsList();
// 放到request域中
model.addAttribute("itemList",items);
//return "itemList"; // 直接返回一个视图文件(jsp)
//return "redirect:/itemEdit.action?itemId=1"; // 重定向到其他URL
return "forward: /itemEdit.action?itemId=1"; // 转发到其他URL
}
3、返回Void(常用于ajax)
/ 配置请求的url
@RequestMapping(value = "/itemList.action")
public void queryItemList(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model) throws ServletException, IOException {
// 创建页面需要显示的商品数据
List<Item> items = itemService.selectItemsList();
request.setAttribute("itemList",items);
// 请求转发
request.getRequestDispatcher("/WEB-INF/jsp/itemList.jsp").forward(request,response);
}
四.异常处理器
springmvc在处理请求过程中出现异常信息交由异常处理器进行处理,自定义异常处理器可以实现一个系统的异常处理逻辑。
系统中异常包括两类:预期异常和运行时异常RuntimeException,前者通过捕获异常从而获取异常信息,后者主要通过规范代码开发、测试通过手段减少运行时异常的发生。
系统的dao、service、controller出现都通过throws Exception向上抛出,最后由springmvc前端控制器交由异常处理器进行异常处理,如下图:
自定义异常处理器
1)自定义一个异常MyException.java
public class MyException extends Exception {
private String msg;
public MyException(String msg) {
this.msg = msg;
}
public MyException() {
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
2)在程序中手动抛出这个异常
// 配置请求的url
@RequestMapping(value = "/itemList.action")
public void queryItemList(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model) throws Exception {
if (1==1) {
throw new MyException("啦啦啦");
}
// 创建页面需要显示的商品数据
List<Item> items = itemService.selectItemsList();
request.setAttribute("itemList",items);
// 请求转发
request.getRequestDispatcher("/WEB-INF/jsp/itemList.jsp").forward(request,response);
}
3)在applicationContext.xml中配置全局异常处理器
<!-- *****************配置全局异常处理器***************** --> <bean class="cn.x5456.exception.CustomExceptionResolver"/>
4)书写异常处理器
import org.springframework.web.servlet.HandlerExceptionResolver; // 别导错包
public class CustomExceptionResolver implements HandlerExceptionResolver {
@Override
public ModelAndView resolveException(HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse,
Object o, // 发生异常的地方 Serivce层 方法 包名+类名+方法名(形参) 字符串
Exception e) {
ModelAndView mav = new ModelAndView();
String msg = "未知异常";
if (e instanceof MyException){
MyException myex = (MyException) e;
msg = myex.getMsg();
}
mav.addObject("msg",msg);
mav.setViewName("error");
return mav;
}
}