异常处理器
系统中的异常类型:
预期可能发生的异常,代码处理时可以 try/catch,向上抛出异常;
运行时异常:
异常处理方案:
- 新建异常处理类 ExceptionResolver.java实现HandlerExceptionResolver接口; 自定义异常类MyException.java;
- springmvc.xml 中配置统一异常处理器;
- 异常处理逻辑:
① 根据不同的异常类型进行异常处理;
② 自定义异常类(ExceptionResolver),在 Hnadler,Service方法中手动抛出此类异常;
③ 针对系统自定义异常,可以直接从异常类获取到异常信息,将异常在错误页面中显示;
针对非自定义异常,将这类重新构造成自定义类型,异常信息为“位置异常”。
eg:
1. springmvc.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 定义统一异常处理器 -->
<bean class="com.asd.exception.ExceptionResolver"></bean>
</beans>
2. ExceptionResolver.java
public class ExceptionResolver implements HandlerExceptionResolver {
public ModelAndView resolveException(HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse, Object o, Exception e) {
System.out.println("统一处理异常位置");
if (e instanceof MyException) {
System.out.println("跳转到网络连接错误页面");
} else if (e instanceof NullPointerException) {
System.out.println("跳转到请稍后再试");
} else if (e instanceof NullPointerException) {
System.out.println("跳转到商品已下架");
} else {
System.out.println("请再试打开");
}
ModelAndView mv = new ModelAndView();
mv.setViewName("main/error");
mv.addObject("errorMsg", "报错了");
return mv;
}
}
3. MyException.java
public class MyException extends RuntimeException {
public MyException(String message) {
super(message);
}
}
4. error.jsp
<body>
${message}
</body>
5. ItemsController.java
(如果不想在Controller层处理,可以注释掉 if(id<0);再在Service层做处理;)
@Controller
@RequestMapping("/items")
public class ItemsController{
@Autowired
private ItemsService itemsService;
@RequestMapping(value="/editItems",method=RequestMethod.GET)
public String editItems(Model model,Integer id)throws Exception(){
/* if (id<0) {
throws new MyException("id 无效");
} */
model.addAttribute("id",id);
//通过id查询商品
ItemsCustom itemsCustom=itemsService.findItemsById(id);
model.addAttribute("itemsCustom",itemsCustom);
return "editItems";
}
}
5’. ItemsSevice.java
@Controller
@RequestMapping("/items")
public class ItemsServiceImpl implements ItemsService{
@Autowired
private ItemsCustomMapper itemsCustomMapper;
@Autowired
private ItemsMapper itemsMapper;
public List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo)throws Exception{
return itemsCustomMapper.findItemsList(itemsQueryVo);
}
public ItemsCustom findItemsById(int id) throws Exception{
Items items=itemsMapper.selectByPrimaryKey(id);
//需求查询其他相关信息
ItemsCustom itemsCustom=new ItemsCustom();
/* if (items==null) {
throws new MyException("商品不存在");
} */
//将items属性的值,拷贝到itemsCustom
BeanUtil.copyProperties(items,itemsCustom);
return itemsCustom;
}
}