SpringMVC处理异常:

  1 使用@ExceptionHandler注解

    只有ModelAndView可以携带错误信息

    @ExceptionHandler
public ModelAndView handleException(Exception ex) {
System.out.println("出现异常:" + ex);
ModelAndView mv = new ModelAndView();
mv.setViewName("error");//转到error.jsp
mv.addObject("exception", ex);
return mv;
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" isErrorPage="true"%>

<!-- 打印异常信息 -->
<%=exception.getMessage() %>

${exception.message}

  2 使用@ExceptionHandler处理特定类型的异常

public class LackParamException extends ServletException{

   private static final long serialVersionUID = 1L;

   public LackParamException() {
super("缺少参数");
} public LackParamException(String msg) {
super(msg);
}
}
    @ExceptionHandler(LackParamException.class)
public ModelAndView handleException(Exception ex) {
System.out.println("出现异常:" + ex);
ModelAndView mv = new ModelAndView();
mv.setViewName("error");
mv.addObject("exception", ex);
return mv;
} @RequestMapping("test_exception")
public String testException(String str) throws LackParamException {
if(StringUtils.isEmpty(str)) {
throw new LackParamException();
}
return "success";
}

  3 使用@ControllerAdvice织入全局异常

//异常切面:将本类内部的@ExceptionHandler方法织入到所有的@RequestMapping方法
@ControllerAdvice
public class MyExceptionHandler {
@ExceptionHandler(Exception.class)
public ModelAndView handleException(Exception ex) {
System.out.println("出现全局异常:" + ex);
ModelAndView mv = new ModelAndView();
mv.setViewName("error");
mv.addObject("exception", ex);
return mv;
}
}
 <!-- 扫描异常处理 -->
<context:component-scan base-package="com.roxy.springmvc.exception">
</context:component-scan>

Spring MVC 处理异常的更多相关文章

  1. Spring MVC处理异常的4种方式

    http://blog.csdn.net/ufo2910628/article/details/40399539 http://my.oschina.net/CandyDesire/blog/3333 ...

  2. Spring MVC 处理异常的3种方式

    使用Spring MVC开发的博客网站时,遇到了如何处理业务层抛出的异常的问题,查阅到了spring官方博客-spring MVC中异常的处理,以下将会以登录模块为示例. 愚蠢的处理方式 处理异常遵循 ...

  3. Spring mvc 简单异常配置jsp页面

    原文出处:http://howtodoinjava.com/spring/spring-mvc/spring-mvc-simplemappingexceptionresolver-example/ 这 ...

  4. Spring MVC全局异常后返回JSON异常数据

    问题: 当前项目是作为手机APP后台支持,使用spring mvc + mybaits + shiro进行开发.后台服务与手机端交互是发送JSON数据.如果后台发生异常,会直接返回异常页面,显示异常内 ...

  5. Spring MVC 单元测试异常 Caused by: org.springframework.core.NestedIOException: ASM ClassReader failed to parse class file

    Sping 3.2.8.RELEASE + sping mvc + JDK 1.8运行异常. java.lang.IllegalStateException: Failed to load Appli ...

  6. Spring MVC的异常统一处理方法

    我们经常需要统一配置项目的异常处理,又希望统一处理异常代码,同时不侵入原有的正常代码.我们可以通过以下三种方式实现统一处理项目的自定义异常. 通过SimpleMappingExceptionResol ...

  7. Spring MVC报异常:org.springframework.web.util.NestedServletException: Request processing failed

    在使用SpringMVC绑定基本类型(如String,Integer等)参数时,应通过@RequestParam注解指定具体的参数名称,否则,当源代码在非debug模式下编译后,运行时会引发Handl ...

  8. 部署项目到linux中报Spring MVC报异常:org.springframework.web.util.NestedServletException: Request processing failed

    @RequestMapping(value = "/security/login", method = RequestMethod.POST) public ModelAndVie ...

  9. Spring MVC处理异常有3种方法

    1.使用 SimpleMappingExceptionResolver 实现异常处理 <bean class="org.springframework.web.servlet.hand ...

随机推荐

  1. Load Balancing OpenSSH SFTP with HAProxy

    In my previous post I described how we setup a Ubuntu Server (12.0.4) as an OpenSSH SFTP server. In ...

  2. 关于宽带接两台路由,并且第二台需要关闭DHCP的设置

    关于宽带接两台路由,并且第二台需要关闭DHCP的设置 https://wenku.baidu.com/view/e317a12d4b35eefdc8d333cb?pcf=2#1

  3. Bjarne Stroustrup announces C++ Core Guidelines

    This morning in his opening keynote at CppCon, Bjarne Stroustrup announced the C++ Core Guidelines ( ...

  4. git 项目配置用户名、邮箱的方法

    git 项目配置用户名.邮箱的方法 单个仓库里,配置用户名.邮箱: git config user.name "姓名" git config user.email "邮箱 ...

  5. npm install 包 失败解决方法

    在ionic 安装包的时候 npm install xxx --save 出现如下错误 查看日志文件 14 verbose stack SyntaxError: Unexpected token &l ...

  6. NET设计模式 第二部分 结构性模式(10):组合模式(Composite Pattern)

    组合模式(Composite Pattern) ——.NET设计模式系列之十一 Terrylee,2006年3月 概述 组合模式有时候又叫做部分-整体模式,它使我们树型结构的问题中,模糊了简单元素和复 ...

  7. cocos 3.16 生成预编译库模板

    打开模板功能 不知道什么原因,cocos 3.16关闭了模板功能,因此使用cocos -h命令时,无法显示gen-templates参数. 打开的方式也挺简单,打开tools/cocos2d-cons ...

  8. CentOS7局域网下安装离线Ambari

    1 Ambari介绍.安装与应用案例介绍 1.1 Ambari Ambari 跟 Hadoop 等开源软件一样,也是 Apache Software Foundation 中的一个项目,并且是顶级项目 ...

  9. Linux之chgrp

    命令功能: 在lunix系统里,文件或目录的权限的掌控以拥有者及所诉群组来管理.可以使用chgrp指令取变更文件与目录所属群组,这种方式采用群组名称或群组识别码都可以.Chgrp命令就是change  ...

  10. mwan3多wan叠加成功

    参考: http://www.right.com.cn/forum/forum.php?mod=viewthread&tid=147109&highlight=mwan3 wan口为路 ...