b2科目四模拟试题多少题驾考考爆了怎么补救
b2科目四模拟试题多少题 驾考考爆了怎么补救

使用Spring MVC统一异常处理实战

电脑杂谈  发布时间:2019-08-15 09:02:10  来源:网络整理

处理的日志时出错_springmvc日志处理_springmvc日志处理

1 描述

在J2EE项目的研发中,还是是对上层的操作过程,不过业务层的处理过程,不过控制层的处理过程,都不可避免会遇见这些可预知的、不可预知的异常需要处理。每个过程都单独处理异常,功能的源码耦合度高,工作量大且不好统一,维护的工作量也很大。

假如,能不能将一切类型的异常处理从各处理过程解耦出来,如此既确保了相关处理过程的系统较单一,也实现了异常信息的统一处理和维护?答案是肯定的。下面将介绍使用Spring MVC统一处理异常的解决和实现过程。

2 分析

Spring MVC处理异常有3种方式:

(1)使用Spring MVC提供的简单异常处理器SimpleMappingExceptionResolver;

(2)实现Spring的异常处理接口HandlerExceptionResolver 自定义自己的异常处理器;

(3)使用@ExceptionHandler注解实现异常处理;

3 实战

处理的日志时出错_springmvc日志处理_springmvc日志处理

3.1 引言

为了验证Spring MVC的3种异常处理手段的实际作用,我们需要开发一个测试项目,从Dao层、Service层、Controller层分别抛出不同的异常,然后分别集成3种方式进行异常处理,因而相对3种方式的优缺点。

3.2 实战项目

3.2.1 项目结构

#db2d82b84efee499fe6689c8a6aa7ea#

3.2.2 异常类定义

/** 
 * 系统业务异常 
 */  
public class BusinessException extends RuntimeException {  
  
    /** serialVersionUID */  
    private static final long serialVersionUID = 2332608236621015980L;  
  
    private String code;  
  
    public BusinessException() {  
        super();  
    }  
  
    public BusinessException(String message) {  
        super(message);  
    }  
  
    public BusinessException(String code, String message) {  
        super(message);  
        this.code = code;  
    }  
  
    public BusinessException(Throwable cause) {  
        super(cause);  
    }  
  
    public BusinessException(String message, Throwable cause) {  
        super(message, cause);  
    }  
  
    public BusinessException(String code, String message, Throwable cause) {  
        super(message, cause);  
        this.code = code;  
    }  
  
    public String getCode() {  
        return code;  
    }  
  
    public void setCode(String code) {  
        this.code = code;  
    }  
  
}  
  
  
public class ParameterException extends RuntimeException {  
  
    /** serialVersionUID */  
    private static final long serialVersionUID = 6417641452178955756L;  
  
    public ParameterException() {  
        super();  
    }  
  
    public ParameterException(String message) {  
        super(message);  
    }  
  
    public ParameterException(Throwable cause) {  
        super(cause);  
    }  
  
    public ParameterException(String message, Throwable cause) {  
        super(message, cause);  
    }  
}

3.2.3 Dao层代码

@Repository("testDao")
public class TestDao {
	public void exception(Integer id) throws Exception {
		switch(id) {
		case 1:
			throw new BusinessException("12", "dao12");
		case 2:
			throw new BusinessException("22", "dao22");
		case 3:
			throw new BusinessException("32", "dao32");
		case 4:
			throw new BusinessException("42", "dao42");
		case 5:
			throw new BusinessException("52", "dao52");
		default:
			throw new ParameterException("Dao Parameter Error");
		}
	}
}

springmvc日志处理_处理的日志时出错_springmvc日志处理

3.2.4 Service层代码

public interface TestService {
	public void exception(Integer id) throws Exception;
	
	public void dao(Integer id) throws Exception;
}
@Service("testService")
public class TestServiceImpl implements TestService {
	@Resource
	private TestDao testDao;
	
	public void exception(Integer id) throws Exception {
		switch(id) {
		case 1:
			throw new BusinessException("11", "service11");
		case 2:
			throw new BusinessException("21", "service21");
		case 3:
			throw new BusinessException("31", "service31");
		case 4:
			throw new BusinessException("41", "service41");
		case 5:
			throw new BusinessException("51", "service51");
		default:
			throw new ParameterException("Service Parameter Error");
		}
	}
	@Override
	public void dao(Integer id) throws Exception {
		testDao.exception(id);
	}
}

3.2.5 Controller层代码

@Controller
public class TestController {
	@Resource
	private TestService testService;
	
	@RequestMapping(value = "/controller.do", method = RequestMethod.GET)
	public void controller(HttpServletResponse response, Integer id) throws Exception {
		switch(id) {
		case 1:
			throw new BusinessException("10", "controller10");
		case 2:
			throw new BusinessException("20", "controller20");
		case 3:
			throw new BusinessException("30", "controller30");
		case 4:
			throw new BusinessException("40", "controller40");
		case 5:
			throw new BusinessException("50", "controller50");
		default:
			throw new ParameterException("Controller Parameter Error");
		}
	}
	
	@RequestMapping(value = "/service.do", method = RequestMethod.GET)
	public void service(HttpServletResponse response, Integer id) throws Exception {
		testService.exception(id);
	}
	
	@RequestMapping(value = "/dao.do", method = RequestMethod.GET)
	public void dao(HttpServletResponse response, Integer id) throws Exception {
		testService.dao(id);
	}
}

3.2.6 JSP页面代码

<%@ page contentType="text/html; charset=UTF-8"%>
<html>
<head>
<title>Maven Demo</title>
</head>
<body>
<h1>所有的演示例子</h1>
<h3>[url=./dao.do?id=1]Dao正常错误[/url]</h3>
<h3>[url=./dao.do?id=10]Dao参数错误[/url]</h3>
<h3>[url=./dao.do?id=]Dao未知错误[/url]</h3>
<h3>[url=./service.do?id=1]Service正常错误[/url]</h3>
<h3>[url=./service.do?id=10]Service参数错误[/url]</h3>
<h3>[url=./service.do?id=]Service未知错误[/url]</h3>
<h3>[url=./controller.do?id=1]Controller正常错误[/url]</h3>
<h3>[url=./controller.do?id=10]Controller参数错误[/url]</h3>
<h3>[url=./controller.do?id=]Controller未知错误[/url]</h3>
<h3>[url=./404.do?id=1]404错误[/url]</h3>
</body>
</html>


本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-119312-1.html

相关阅读
    发表评论  请自觉遵守互联网相关的政策法规,严禁发布、暴力、反动的言论

    热点图片
    拼命载入中...