spring3.0开发d异常处理,如果只有jsp请求能捕获到异常,是不足够的, 有时候AJAX返回json数据时遇到异常。这时候默认的处理方式不能满足了, 需要自定义的方式支持同步和ajax异步异常处理。
Spring3.0中对异常的处理方法一共提供了两种:一种是使用HandlerExceptionResolver接口;一种是在Controller类内部使用@ExceptionHandler注解。使用第一种方式可以实现全局异常控制,并且Spring已经提供了一个默认的实现类SimpleMappingExceptionResolver;使用第二种方式可以在Controller内部实现更个性化的异常处理方式,灵活性更高。一般来说,项目中只需要采用第一种方式就可以了,每个人都自己定义异常的展现方式,太过个性不统一。
以配置文件的方式进行异常管理
在配置dispacher-servlet.xml
配置多个异常处理的地方。
以注解的方式配置异常管理
为了方便阅读, 把代码发全一点
- @Controller
- public class ExceptionHandlerController
- {
- private final Logger log = LoggerFactory.getLogger(getClass());
- @ExceptionHandler(ManagerSecurityException.class)
- public ModelAndView handleManagerSecurityException(ManagerSecurityException e,
- HttpServletResponse response)
- throws IOException
- {
- log.info("Manager exception handler " + e.getMessage());
- response.sendError(HttpServletResponse.SC_FORBIDDEN,
- e.getMessage());
- return new ModelAndView("viewName",new HashMap());
- }
- }
这样的配置处理jstl请求jsp没有问题, 如果是jquery的 $.post的方法请求, 那么如果还是返回 error.jsp ,那在 js的success函数里则不能很好地输出显示。
问题又来了: 如果在 handleManagerSecurityException方法的上面打上注解 @ResponeBody 那么这样又只能返回 json格式串的异常处理, 你想同步请求跳到到error.jsp又不行啦!
解决办法是这样的:定制化异常处理
下面我们来看看CustomSimpleMappingExceptionResolver.java是如何做到支持JSP和JSON格式返回的异常错误的
- @Override
- protected ModelAndView doResolveException(HttpServletRequest request,
- HttpServletResponse response,
- Object handler,
- Exception ex) {
- // Expose ModelAndView for chosen error view.
- String viewName = determineViewName(ex, request);
- if (viewName != null) {//JSP格式返回
- if(!(request.getHeader("accept").indexOf("application/json")>-1 || request.getHeader("X-Requested-With").indexOf("XMLHttpRequest")>-1)){//如果不是异步请求
- // Apply HTTP status code for error views, if specified.
- // Only apply it if we're processing a top-level request.
- Integer statusCode = determineStatusCode(request, viewName);
- if (statusCode != null) {
- applyStatusCodeIfPossible(request, response, statusCode);
- return getModelAndView(viewName, ex, request);
- }
- }else{//JSON格式返回
- Map model=new HashMap();
- if(this.logger.isDebugEnabled()){
- model.put("debug", true);
- }//exception
- model.put(ConfigConstants.RESULT, ex.getMessage());
- model.put("failure", true);
- try {
- response.getWriter().write("有异常啦!");
- } catch (IOException e) {
- e.printStackTrace();
- }
- return new ModelAndView();
- }
- return null;
- }
- else {
- return null;
- }
- }
这里判断 request.getHeader("accept").indexOf("application/json") 是不是异步请求
下面给出同步和异步请求的Header
同步的text/html
=== MimeHeaders ===
host = localhost:8888
user-agent = Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8
accept = text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
accept-language = en,zh;q=0.7,en-gb;q=0.3
accept-encoding = gzip,deflate
accept-charset = ISO-8859-1,utf-8;q=0.7,*;q=0.7
keep-alive = 115
connection = keep-alive
cookie = JSESSIONID=BB7441E4B481FF64A5BCC8E6F596C330
异步的application/json
=== MimeHeaders ===
host = localhost:8888
user-agent = Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8
accept = application/json, text/javascript, */*
accept-language = en,zh;q=0.7,en-gb;q=0.3
accept-encoding = gzip,deflate
accept-charset = ISO-8859-1,utf-8;q=0.7,*;q=0.7
keep-alive = 115
connection = keep-alive
content-type = application/x-www-form-urlencoded
x-requested-with = XMLHttpRequest
referer = http://localhost:8888/auth/auth/dictionary/dictionaryForm.html
cookie = JSESSIONID=A4B59EA87C9B83B71C0D455634746411
请注意
try {
response.getWriter().write("有异常啦!");
} catch (IOException e) {
e.printStackTrace();
}
return new ModelAndView();
关键就在最后这1句 return new ModelAndView();
为什么不写 return null; 或者 return new ModelAndView('jsonView', model); 呢?
因为spring3的源码里这样写着了
- protected ModelAndView processHandlerException(HttpServletRequest request,
- HttpServletResponse response,
- Object handler,
- Exception ex) throws Exception {
- // Check registerer HandlerExceptionResolvers...
- ModelAndView exMv = null;
- for (HandlerExceptionResolver handlerExceptionResolver : this.handlerExceptionResolvers) {
- exMv = handlerExceptionResolver.resolveException(request, response, handler, ex);
- if (exMv != null) {
- break;
- }
- }
- if (exMv != null) {
- if (exMv.isEmpty()) {
- return null;
- }
- if (logger.isDebugEnabled()) {
- logger.debug("Handler execution resulted in exception - forwarding to resolved error view: " + exMv,
- ex);
- }
- WebUtils.exposeErrorRequestAttributes(request, ex, getServletName());
- return exMv;
- }
- throw ex;
- }
注意看
if (exMv != null) {
if (exMv.isEmpty()) {
return null;
}
这句和最后一句: throw ex;
如果你写 respone.getWriter().write("有异常啦"); 你也要 new一个空的ModelAndView,上面的代码表明, 如果你 return null; 它就跑出异常 throw ex; 如果没人接收, 则会调用你配置的默认异常处理器 或者中断不做任何输出。
好了, 这样可以同时支持同步和AJAX异步请求异常处理了。
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/9399028/viewspace-1820428/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/9399028/viewspace-1820428/