一、自定义异常类:对于不同的异常类型定义异常类,继承Exception
CustomException.java
package com.sky.ssm.exception;
/**
* 系统自定义异常类
* 针对预期的异常,需要在程序中抛出此类的异常
* @author sk
*
*/
public class CustomException extends Exception{
//异常信息
public String message;
public CustomException(String message){
super(message);
this.message = message;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
二、设置全局的异常处理
思路:当系统遇到异常,在程序中手动抛出,dao抛给service、service抛给controller,controller抛给前端控制器,前端控制器调用全局异常处理器。
全局异常处理器处理思路:
解析出异常类型
如果该异常类型是系统自定义的异常,直接取出异常信息,在错误页面展示
如果该异常类型不是系统自定义的异常,构造一个自定义的异常类型(信息为“未知错误”)
Springmvc提供了一个HandlerExceptionResolver接口,只要实现这个接口,springmvc就会默认为这是一个全局的异常处理。
一个系统中,只能由一个全局的异常处理,
CustomExceptionResolver
package com.sky.ssm.exception;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
/**
* 全局异常处理器
* @author sk
*
*/
public class CustomExceptionResolver implements HandlerExceptionResolver{
/**
* 系统抛出的异常
*/
@Override
public ModelAndView resolveException(HttpServletRequest request,
HttpServletResponse response, Object handler, Exception ex) {
// handler就是处理器适配器要执行Handler对象(只有一个method)
//解析出异常类型
//如果该异常类型是系统自定义的异常,直接取出异常信息,在错误页面展示
// String message = null;
// if(ex instanceof CustomException){
// message = ((CustomException)ex).getMessage();
// }else{//如果该异常类型不是系统自定义的异常,构造一个自定义的异常类型(信息为“未知错误”)
// message = "未知错误";
// }
//上边的代码变为
CustomException customException = null;
if(ex instanceof CustomException){
customException = (CustomException) ex;
}else{//如果该异常类型不是系统自定义的异常,构造一个自定义的异常类型(信息为“未知错误”)
customException = new CustomException("未知错误");
}
//错误信息
String message = customException.getMessage();
//定义modelAndView
ModelAndView modelAndView = new ModelAndView();
//将错误信息传到页面
modelAndView.addObject("message",message);
//指向错误页面
modelAndView.setViewName("error");
return modelAndView;
}
}
三、
在
springmvc.xml
中配置全局异常处理器
<!-- 全局异常处理器 只要实现了HandlerExceptionResolver 接口,就是一个全局的异常处理器 -->
<bean class="com.sky.ssm.exception.CustomExceptionResolver"/>
四、测试异常
在controller方法中抛出异常
@RequestMapping(value="/editItems",method={RequestMethod.POST,RequestMethod.GET})
public String editItems(Model model, @RequestParam(value="id",required=true,defaultValue="1")Integer items_id) throws Exception{
//调用service查询商品信息
ItemsCustom itemsCustom = itemsService.findItemsById(items_id);
if(itemsCustom == null){
//抛出异常
throw new CustomException("controller异常:商品信息输入有误!");
}
//通过形参中的model将model数据传到页面
//相当于modelAndView.addObject方法
model.addAttribute("itemsCustom",itemsCustom);
return "items/editItems";
}
在service中抛出异常
@Override
public ItemsCustom findItemsById(int id) throws Exception {
Items items = itemsMapper.selectByPrimaryKey(id);
if(items == null){
throw new CustomException("service异常:商品信息为空!");
}
//中间对商品信息进行业务处理
//.......
//返回itemsCustom
ItemsCustom itemsCustom = null;
if(items != null){
itemsCustom = new ItemsCustom();
//将items中的属性值拷贝到itemsCustom
BeanUtils.copyProperties(items, itemsCustom);
}
return itemsCustom;
}
不论实在service中还是在controller中抛出的自定义的异常,springmvc都会去调用自定义的异常处理器进行异常的处理
当然,如果与业务功能相关的异常,建议在service中抛出异常
与业务功能没有关系的异常,建议在controller中抛出异常
项目结构