Spring MVC的SimpleMappingExceptionResolver处理异常

一 视图

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>异常处理示例</title>
</head>
<body>

<a href="hello">没有异常处理</a>

<a href="test">使用简单异常处理器处理异常</a>

<a href="find">使用简单异常处理器处理特定异常</a>

</body>
</html>

error.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>异常处理示例</title>
</head>
<body>
<h3>异常处理页面</h3>
抛出异常信息:${requestScope.ex.message}
</body>
</html>

sqlerror.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>异常处理示例</title>
</head>
<body>
<h3>特定异常处理页面</h3>
抛出异常信息:${requestScope.ex.message}
</body>
</html>

二 控制器

package org.fkit.controller;


import java.sql.SQLException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class TestController{
    
    @GetMapping("/hello")
    public String hello() throws Exception{
        // 抛出异常
        throw new Exception();
    }
    
    @GetMapping("/test")
    public String test() throws Exception{
        // 模拟异常
        int i = 5/0;
        return "success";
    }
    
    @GetMapping("/find")
    public String find() throws Exception{
        try {
            // 模拟异常
            int i = 5/0;
            return "success";
        } catch (Exception e) {
            throw new SQLException("查找数据失败!");
        }
        
    }
    
}

三 配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
     xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd     
        http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context.xsd">
        
    <!-- spring可以自动去扫描base-pack下面的包或者子包下面的java文件,
      如果扫描到有Spring的相关注解的类,则把这些类注册为Spring的bean  -->
    <context:component-scan base-package="org.fkit.controller"/>
    <!-- 默认配置方案 -->
    <mvc:annotation-driven/>
     <!-- 静态资源处理 -->
    <mvc:default-servlet-handler/>
    
    <!-- 视图解析器  p:prefix属性表示前缀  p:suffix 表示后缀  -->
     <bean id="viewResolver"
           class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          p:prefix="/WEB-INF/content/" p:suffix=".jsp"/>
    
    <!-- 异常处理
      p:defaultErrorView="error"表示所有没有指定的异常,都跳转到异常处理页面error
      p:exceptionAttribute="ex"表示异常处理页面中访问的异常对象变量名是ex
     -->
    <bean  class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"
      p:defaultErrorView="error"
      p:exceptionAttribute="ex">
      <!-- 异常映射
           exceptionMappings表示映射的异常,接受参数是一个Properties
           key是异常类名,value是处理异常的页面
      -->
      <property name="exceptionMappings">
           <props>
                <prop key="IOException">ioerror</prop>
                <prop key="SQLException">sqlerror</prop>
           </props>
      </property>
    </bean>
    
</beans>

四 测试

点击第1个链接

点击第2个链接

点击第3个链接

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值