【SpringMVC整合MyBatis】springmvc异常处理-全局异常处理器开发

异常处理
1.异常处理思路

系统中异常包括两类:预期异常和运行时异常RuntimeException,前者通过捕获异常从而获取异常信息,后者主要通过规范代码开发、测试通过手段减少运行时异常的发生。
系统的dao、service、controller出现都通过throws Exception向上抛出,最后由springmvc前端控制器交由异常处理器进行异常处理,如下图:



springmvc提供全局异常处理器(一个系统只有一个异常处理器)进行统一异常处理。

2.自定义异常类

对不同的异常类型定义异常类,继承Exception。
package cn.edu.hpu.ssm.exception;

//系统自定义异常处理类,针对预期的异常,需要在程序中抛出此类的异常
public class CustomException extends Exception{
	
	//异常信息
	private String message;
	
	public CustomException(String message){
		super(message);
		this.message=message;
	}


	public String getMessage() {
		return message;
	}


	public void setMessage(String message) {
		this.message = message;
	}
}

3.全局异常处理器

思路:
系统遇到异常,在程序中手动抛出,dao抛给service、service给controller、controller抛给前端控制器,前端控制器调用全局异常处理器。

全局异常处理器处理思路:
    解析出异常类型。
    如果该 异常类型是系统 自定义的异常,直接取出异常信息,在错误页面展示。
    如果该 异常类型不是系统 自定义的异常,构造一个自定义的异常类型(信息为“未知错误”)。

springmvc提供一个HandlerExceptionResolver接口
package cn.edu.hpu.ssm.exception;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

//全局异常处理器
public class CustomExceptionResolver implements HandlerExceptionResolver{

	//系统抛出的异常
	@Override
	public ModelAndView resolveException(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex) {
		//handler就是处理器适配器要执行的Handler对象(只有method)
		//解析出异常类型。
		
	    //如果该 异常类型是系统 自定义的异常,直接取出异常信息,在错误页面展示。
		CustomException customException=null;
		if(ex instanceof CustomException){
			customException=(CustomException)ex;
	    	
	    }else{
	    	//如果该 异常类型不是系统 自定义的异常,构造一个自定义的异常类型(信息为“未知错误”)。
	    	customException=new CustomException("未知错误");
	    }

		//错误信息
		String message=customException.getMessage();
		
		ModelAndView modelAndView=new ModelAndView();
		
		//将错误信息传到页面
		modelAndView.addObject("message",message);
		
		//指向到错误界面
		modelAndView.setViewName("error");
		
		return modelAndView;
	}
	 
}

4.错误页面
在WEB-INF/jsp文件夹下创建error.jsp页面,内容为:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>错误提示</title>


  </head>
  
  <body>
    <h1><font color="red">${message }</font></h1><br>
  </body>
</html>

5.在springmvc.xml配置全局异常处理器
<!-- 全局异常处理器 
	只要你实现了HandlerExceptionResolver接口,这个
	类就是一个全局异常处理器-->
	<bean class="cn.edu.hpu.ssm.exception.CustomExceptionResolver"></bean>

6.异常测试
在controller、service、dao中任意一处需要手动抛出异常。
如果是程序中手动抛出的异常,在错误页面中显示自定义的异常信息,如果不是手动抛出异常说明是一个运行时异常,在错误页面只显示“未知错误”。

在商品修改的controller方法中抛出异常。
@RequestMapping(value="/editItems",method={RequestMethod.POST,RequestMethod.GET})
//@RequestParam里面指定reuqest传入参数和形参进行绑定。
//通过required属性指定参数是否必须要传入
//通过defaultValue可以设置默认值,如果id参数没有传入,将默认值和形参绑定
	public String editItems(Model model,@RequestParam(value="id",required=true,defaultValue="") Integer items_id)throws Exception{
		
		//调用service根据商品id查询商品信息
		ItemsCustom itemsCustom=itemsService.findItemsById(items_id);
		//判断商品是否为空,根据id没有查到商品,提示用户商品信息并不存在
		if(itemsCustom==null){
			throw new CustomException("商品的修改信息不存在!");
		}
		
		//通过形参中的model将model数据传到页面
		//相当于modelAndView.addObject方法
		model.addAttribute("items22",itemsCustom);
		
		return "items/editItems";
	}

其中上面用到的service方法:
@Override
public ItemsCustom findItemsById(Integer id) throws Exception {
	
	Items items=itemsMapper.selectByPrimaryKey(id);
	//中间对商品信息进行业务处理
	//...
	//最终返回ItemsCustom
	ItemsCustom itemsCustom=null;
	//将item的内容拷贝到itemsCustom
	if(items!=null){
		itemsCustom=new ItemsCustom();
		BeanUtils.copyProperties(items, itemsCustom);
	}
	
	return itemsCustom;
}

我们让id指定一个没有的数(如4444),则会抛出我们自定义的异常信息:

同样在Service中也可以抛出异常

如果与业务功能相关的异常,建议在service中抛出异常。
与业务功能没有关系的异常,建议在controller中抛出。

上边的功能,建议在service中抛出异常。

转载请注明出处:http://blog.csdn.net/acmman/article/details/47604099

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值