出自:https://blog.csdn.net/goodyuedandan/article/details/62420120

一、Spring MVC处理异常有3种方式: 
(1)使用Spring-MVC提供的SimpleMappingExceptionResolver;
(2)实现Spring的异常处理接口HandlerExceptionResolver 自定义自己的异常处理器;

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

Spring HandlerExceptionResolver implementations deal with unexpected exceptions that occur during controller execution. A HandlerExceptionResolver somewhat resembles the exception mappings you can define in the web application descriptor web.xml. However, they provide a more flexible way to do so. For example they provide information about which handler was http://blog.csdn.net/frankcheng5143/article/details/50890118executing when the exception was thrown. Furthermore, a programmatic way of handling exceptions gives you more options for responding appropriately before the request is forwarded to another URL (the same end result as when you use the Servlet specific exception mappings).

Besides implementing the HandlerExceptionResolver interface, which is only a matter of implementing the resolveException(Exception, Handler) method and returning a ModelAndView, you may also use the provided SimpleMappingExceptionResolver or create @ExceptionHandler methods. The SimpleMappingExceptionResolver enables you to take the class name of any exception that might be thrown and map it to a view name. This is functionally equivalent to the exception mapping feature from the Servlet API, but it is also possible to implement more finely grained mappings of exceptions from different handlers. The @ExceptionHandler annotation on the other hand can be used on methods that should be invoked to handle an exception. Such methods may be defined locally within an @Controller or may apply to many @Controller classes when defined within an @ControllerAdvice class. The following sections explain this in more detail.

在Controller执行过程中发生的异常可以通过Spring的HandlerExceptionResolver实现类来处理异常,它有点像web.xml中你所能定义的那种错误页面。然而,它提供的方式比web.xml更加灵活性。比如它能提供发生异常所在handler的信息,而且通过编程处理异常在转发请求的时候给开发者更多的选择。除了通过实现HandlerExceptionRresolver接口来处理异常之外(它通常是实现resolveException(Exception, Handler)方法并返回一个ModelAndView)开发人员还可以使用SimpleMappingExceptionResolver 或者@ExceptionHandler来处理异常。开发者可以通过SimpleMappingExceptionResolver将异常的类名和一个视图名映射起来,这等同于通过Servlet API的映射, 但是它可以实现更加细粒度的将异常映射到不同的handler上。 在Controller中,可以通过@ExceptionHandler注解来处理异常@ExceptionHandler注解在方法之上,将或者通过@ControllerAdvice定义在许多的Controller上。

(1)使用SimpleMappingExceptionResolver实现异常处理,能够轻松的将异常映射到对应的错误视图上,在处理请求的时候Spring MVC可能抛出很多异常,给客户端返回信息的时候需要和相关的错误码匹配,根据错误给客户的返回相关的错误代号;

在springMvc-content.xml配置文件如下:

    <!-- shiro异常处理 -->
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<prop key="org.apache.shiro.authz.AuthorizationException">/unauthor</prop>
<prop key="java.lang.Throwable">error/500</prop>
</props>
</property>
</bean>

DefaultHandlerExceptionResolver将SpringMVC的异常分发到不同的错误代码上,下面是错误代码和异常的对应关系:

(2) 实现HandlerExceptionResolver 接口自定义异常处理器,HandlerExceptionResolver是一个接口,只有一个方法,我们只需要实现这个接口;

在springMvc-content.xml配置文件如下:

<!--然后通过 Spring的HandlerExceptionResolver去进行全局捕获,不论你在系统哪里去throw,只要实现了 HandlerExceptionResovler这个接口,Spring都会拦截下异常进行处理  -->
<bean id="exceptionResolver" class="com.lf.resolver.MyExceptionResolver"></bean>

我的实现如下:

public class MyExceptionResolver implements HandlerExceptionResolver {

   public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
System.out.println("==============异常开始=============");
ex.printStackTrace();
System.out.println("==============异常结束=============");
ModelAndView mv = new ModelAndView("error");
mv.addObject("exception", ex.toString().replaceAll("\n", "<br/>"));
return mv;
} }

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

将@ExceptionHandler标注在Controller的方法上,该方法将处理由@RequestMapping方法抛出的异常。

首先要增加BaseController类,并在类中使用@ExceptionHandler注解声明异常处理,代码如下:

public class BaseController {
/** 基于@ExceptionHandler异常处理 */
  @ExceptionHandler
public String exp(HttpServletRequest request, Exception ex) {
request.setAttribute("ex", ex);
// 根据不同错误转向不同页面
if(ex instanceof BusinessException) {
return "business_error";
}else if(ex instanceof ParameterException) {
return "parameter_error";
} else {
return "error";
}
}
}

然后需要修改现有代码,使所有需要异常处理的Controller都继承该类,如下所示:

public class TestController extends BaseController

三、未捕获异常的处理 :

对于Unchecked Exception而言,由于代码不强制捕获,往往被忽略,如果运行期产生了UncheckedException,而代码中又没有进行相应的捕获和处理,则我们可能不得不面对尴尬的404、500……等服务器内部错误提示页面。 
        我们需要一个全面而有效的异常处理机制。目前大多数服务器也都支持在Web.xml中通过<error-page>(Websphere/Weblogic)或者<error-code>(Tomcat)节点配置特定异常情况的显示页面。

实现方式如下:

修改web.xml文件,增加以下内容:

<!-- 出错页面定义 -->
<error-page>
<exception-type>java.lang.Throwable</exception-type>
<location>/500.jsp</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/500.jsp</location>
</error-page>
<error-page>
<error-code>404</error-code>
<location>/404.jsp</location>
</error-page>

三、比较异常处理方式的优缺点:

Spring MVC集成异常处理3种方式都可以达到统一异常处理的目标。从3种方式的优缺点比较:

  若只需要简单的集成异常处理,推荐使用SimpleMappingExceptionResolver即可;

  若需要集成的异常处理能够更具个性化,提供给用户更详细的异常信息,推荐自定义实现HandlerExceptionResolver接口的方式;

  若不喜欢Spring配置文件或要实现“零配置”,且能接受对原有代码的适当入侵,则建议使用@ExceptionHandler注解方式。

在此补充个Ajax实现异常信息无跳转的处理方式:

Java代码(Controller类)

@RequestMapping(value = "/ajaxAlert", method = RequestMethod.POST)
public void ajax(HttpServletResponse response, User user) throws Exception { if(user.getId()==null) {
if(user.getUserName()==null || "".equals(user.getUserName())) {
AjaxUtils.rendJson(response, false, "用户名为空创建失败");
} else {
AjaxUtils.rendJson(response, true, "创建用户成功");
}
} else {
AjaxUtils.rendJson(response, true, "修改用户成功");
}
} @RequestMapping(value = "/controllerAjax", method = RequestMethod.POST)
public void controllerAjax(HttpServletResponse response, Integer id) throws Exception {
try {
testService.daoTest();
AjaxUtils.rendJson(response, true, "操作成功");
} catch(Exception be) {
AjaxUtils.rendJson(response, false, "Dao层异常");
}
}

Java代码(AjaxUtils类)

import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONObject; public class AjaxUtils { public static void rendText(HttpServletResponse response, String content)
throws IOException {
response.setCharacterEncoding("UTF-8");
response.getWriter().write(content);
} public static void rendJson(HttpServletResponse response, boolean success,
String message) throws IOException{
JSONObject json = new JSONObject();
json.put("isSuccess", success);
json.put("message", message);
rendText(response, json.toString());
}
}

页面代码

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Exception</title>
<script type="text/javascript" src="./jquery-1.10.2.min.js"></script>
<script type="text/javascript">
$(function(){
init();
}); function init(){
$("#ajaxCreate").click(function(){
doAjax({userName: "Candy", realName: "Carol"});
})
$("#ajaxUpdate").click(function(){
doAjax({id: 1, userName: "Candy", realName: "Carol"});
})
$("#ajaxFail").click(function(){
doAjax({realName: "Carol"});
})
}
function doAjax(data) {
$.post("./ajax.do",data,function(t){
if(!t.isSuccess){
alert("操作失败, 原因:" + t.message);
}else{
alert("操作成功, 描述:" + t.message);
}
},"json").error(function(){
alert("未知错误");
});
}
</script>
</head>
<body>
<br />
<a id="ajaxCreate" href="#">创建用户成功</a>
<br />
<a id="ajaxUpdate" href="#">修改用户成功</a>
<br />
<a id="ajaxFail" href="#">用户名为空创建失败</a>
</body>
</html>

Shiro异常处理总结的更多相关文章

  1. SpringBoot整合Shiro+MD5+Salt+Redis实现认证和动态权限管理|前后端分离(下)----筑基后期

    写在前面 在上一篇文章<SpringBoot整合Shiro+MD5+Salt+Redis实现认证和动态权限管理(上)----筑基中期>当中,我们初步实现了SpringBoot整合Shiro ...

  2. shiro使用注解(@RequiresPermissions等)不无效及异常处理

    1.注解不生效 在shiro配置类中加上如下代码: /** * Shiro生命周期处理器 */ @Bean(name = "lifecycleBeanPostProcessor") ...

  3. shiro 没有权限异常处理

    <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> ...

  4. Shiro权限框架简单快速入门

    声明本文只适合初学者,本人也是刚接触而已,经过一段时间的研究小有收获,特来分享下希望和大家互相交流学习. 首先配置我们的web.xml代码如下: <filter> <filter-n ...

  5. Shiro固定身份验证

    Shiro基础身份验证 如果要进行shiro的日志信息读取,那么需要使用一个org.apache.shiro.util.Factory接口,在这个接口里面定义有一 取得SecuruityManager ...

  6. Apache shiro的简单介绍与使用(与spring整合使用)

    apache shiro框架简介 Apache Shiro是一个强大而灵活的开源安全框架,它能够干净利落地处理身份认证,授权,企业会话管理和加密.现在,使用Apache Shiro的人越来越多,因为它 ...

  7. 安全框架Shiro入门

    Shiro简介 Apache Shiro是Java的一个安全框架,官网为shiro.apache.org,主要场景为控制登陆,判断用户是否有访问某个功能的权限等等. Shiro的核心功能(入门知识,只 ...

  8. SpringMVC整合Shiro权限框架

    尊重原创:http://blog.csdn.net/donggua3694857/article/details/52157313 最近在学习Shiro,首先非常感谢开涛大神的<跟我学Shiro ...

  9. springboot+shiro+redis项目整合

    介绍: Apache Shiro是一个强大且易用的Java安全框架,执行身份验证.授权.密码学和会话管理.使用Shiro的易于理解的API,您可以快速.轻松地获得任何应用程序,从最小的移动应用程序到最 ...

随机推荐

  1. Codeforces Round #371 & HihoCoder1529【玄学】

    时间限制:40000ms 单点时限:2000ms 内存限制:256MB 描述 给定一个长度为 n 的非负整数序列 a[1..n]. 你每次可以花费 1 的代价给某个 a[i] 加1或者减1. 求最少需 ...

  2. BZOJ1336 Balkan2002 Alien最小圆覆盖 【随机增量法】*

    BZOJ1336 Balkan2002 Alien最小圆覆盖 Description 给出N个点,让你画一个最小的包含所有点的圆. Input 先给出点的个数N,2<=N<=100000, ...

  3. 查询避免Unknown column ‘xxx’ in ‘where clause’

    但凡写过sql语句的人估计都曾经碰到过类似于Unknown column ‘xxx’ in ‘where clause’的问题. 单从字面理解,我们很容易得出列名不存在的结论,但是,很多时候起始并不是 ...

  4. ES6中let和const详解

    let和var一样也是用来定义变量,不同之处在于let是块级作用域,只在所定义的块级作用域中生效,一个花括号便是一个块级作用域 {var a="我是var定义的";let b=&q ...

  5. gradle asciidoc 使用

    备注:    次文档参考github 例子   1.环境准备 node npm (yarn) java KindleGen 备注: 具体的安装可以参考网上相关文档,KindleGen 下载地址:htt ...

  6. 通过openresty && tengine && nginx 动态添加资源到 html 页面

    1. 目标 不用修改后端代码,但是还需要进行js .css 等资源文件的加载 主要的场景是进行统计分析 2.技术实现 通过服务器扩展进行动态添加 iis 可以添加模块,nginx 可以使用  sub_ ...

  7. Word动态替换文本

    public class WordTest2 { public static void main(String[] args) { /** 此Map存放动态替换的内容,key-Word中定义的变量,v ...

  8. 【Python教程】《零基础入门学习Python》(小甲鱼)

    [Python教程]<零基础入门学习Python>(小甲鱼) 讲解通俗易懂,诙谐. 哈哈哈. https://www.bilibili.com/video/av27789609

  9. filter添加水印

    1filter写法 先定义自己的responseWrapper chain.doFilter(request,responseWrapper); responseWrapper来输出 package ...

  10. qing-automation简单入门介绍

    1.相关文档:http://www.51testing.com/html/50/category-catid-250.html 2.进行Qing automation相关操作之前,必须安装好jdk跟a ...