SpringMVC的异常处理,SimpleMappingExceptionResolver只能简单的处理异常

当发生异常的时候,根据发生的异常类型跳转到指定的页面来显示异常信息

ExceptionController.java 处理器

package com.orange.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView; import com.orange.exception.NameException;
import com.orange.exception.PasswordException; @Controller
@RequestMapping("/exception")
public class ExceptionController { @RequestMapping("/simple")
public String doException(){ int i = 3 / 0; return "/showException.jsp";
} }

defaultException.jsp 发生异常跳转的页面

<%@ page language="java" contentType="text/html; charset=GBK"
pageEncoding="GBK"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<base href="<%=basePath %>">
<title>DefaultExceptionPage</title>
</head>
<body>
ERROR! DefaultExceptionPage<br>
message: <c:out value="${ex.message }"></c:out>
</body>
</html>

springMVC配置SimpleMappingExceptionResolver

     <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<!-- 指定所有没有指定的异常,都跳转到该页面 -->
<property name="defaultErrorView" value="/defaultException.jsp" />
<!-- 跳转时携带异常对象 -->
<property name="exceptionAttribute" value="ex"></property>
</bean>

SpringMVC学习笔记四:SimpleMappingExceptionResolver异常处理的更多相关文章

  1. SpringMVC学习笔记(四)

    一.Controller接受网页参数. 1.使用方法的形参来接受 //使用基本类型和字符串来接受 @RequestMapping(value="/param2.do") publi ...

  2. SpringMVC 学习笔记(四) 处理模型数据

    Spring MVC 提供了下面几种途径输出模型数据: – ModelAndView: 处理方法返回值类型为 ModelAndView时, 方法体就可以通过该对象加入模型数据 – Map及Model: ...

  3. SpringMVC学习笔记五:HandlerExceptionResolver异常处理

    继承HandlerExceptionResolver自定义异常处理器 控制器ExceptionController.java package com.orange.controller; import ...

  4. SpringMVC 学习笔记(四)

    41. 尚硅谷_佟刚_SpringMVC_返回JSON.avi SpringMVC中使用@ResponseBody注解标注业务方法,将业务方法的返回值做成json输出给页面 导包: 除了一些sprin ...

  5. SpringMVC学习笔记四:数据绑定

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6831344.html  参考:http://www.cnblogs.com/HD/p/4107674.html ...

  6. springmvc学习笔记--REST API的异常处理

    前言: 最近使用springmvc写了不少rest api, 觉得真是一个好框架. 之前描述的几篇关于rest api的文章, 其实还是不够完善. 比如当遇到参数缺失, 类型不匹配的情况时, 直接抛出 ...

  7. 史上最全的SpringMVC学习笔记

    SpringMVC学习笔记---- 一.SpringMVC基础入门,创建一个HelloWorld程序 1.首先,导入SpringMVC需要的jar包. 2.添加Web.xml配置文件中关于Spring ...

  8. SpringMVC学习笔记之二(SpringMVC高级参数绑定)

    一.高级参数绑定 1.1 绑定数组 需求:在商品列表页面选中多个商品,然后删除. 需求分析:功能要求商品列表页面中的每个商品前有一个checkbok,选中多个商品后点击删除按钮把商品id传递给Cont ...

  9. springmvc学习笔记(简介及使用)

    springmvc学习笔记(简介及使用) 工作之余, 回顾了一下springmvc的相关内容, 这次也为后面复习什么的做个标记, 也希望能与大家交流学习, 通过回帖留言等方式表达自己的观点或学习心得. ...

随机推荐

  1. gcd--最大公因数

    求两个数的最大公倍数 考完五校的第一天,在家补视频ing,简单来说的话就是给了两个数A,B 假设他们两个的最大公倍数为d,那么A=X*d,B=Y*d gcd就是把一直gcd(B%A,A)不断更新,其中 ...

  2. dp--01背包--Charm Bracelet

    Charm Bracelet Bessie has gone to the mall's jewelry store and spies a charm bracelet. Of course, sh ...

  3. UVA 10801 多线程最短路

    题意:一栋摩天大楼从0层到K层,有N部电梯,每个电梯都有自己的运行速度,此外,对于某个电梯来说,并不是每一层都会停,允许在某一层进行电梯换乘,每次换乘固定消耗60秒,最终求从0层去K层的最短时间,如果 ...

  4. D. Minimax Problem(二分+二进制)

    D. Minimax Problem time limit per test 5 seconds memory limit per test 512 megabytes input standard ...

  5. LeetCode No.163,164,165

    No.163 FindMissingRanges 缺失的区间 题目 给定一个排序的整数数组 nums ,其中元素的范围在闭区间 [lower, upper] 当中,返回不包含在数组中的缺失区间. 示例 ...

  6. SpringBoot2.0整合Quartz实现动态设置定时任务时间

    一.    引入依赖 <!-- 引入quartz依赖 --> <dependency> <groupId>org.springframework.boot</ ...

  7. Django-rest framework框架的三大认证组件

    源码分析:三大认证组件的封装 组件的认证配置: 模型层:models.py class User(BaseModel): username = models.CharField(verbose_nam ...

  8. 处理Ajax请求跨域问题

    ajax跨域的原理 ajax出现请求跨域错误问题,主要原因就是因为浏览器的“同源策略”. CORS是一个W3C标准,全称是"跨域资源共享"(Cross-origin resourc ...

  9. input标签添加上disable属性在移动端(ios)字体颜色及边框颜色不兼容的解决办法。

    手机一些兼容性问题: 1.苹果手机输入框input:disabled显示模糊问题 input:disabled, input[disabled]{ color: #5c5c5c; -webkit-te ...

  10. Eclipse Jee 2019-12 使用的一些小窍门(积累性更新)

    在安装使用 Eclipse IDE for Enterprise Java Developers  Version: 2019-12 M1 (4.14.0) 之前请确保已在系统环境变量配置好JDK1. ...