zoukankan      html  css  js  c++  java
  • SpringMVC学习笔记四:SimpleMappingExceptionResolver异常处理

    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> 
  • 相关阅读:
    求解整数集合的交集(腾讯笔试)
    关于屏幕适配之比例布局
    (转)注册JNI函数的两种方式
    正则表达式记录
    当年一个简单可用的多线程断点续传类
    最近用到的几个工具方法
    Android中包含List成员变量的Parcel以及Parcel嵌套写法示例
    java实现计算MD5
    一个用于去除状态栏和虚拟导航栏的BaseActivity
    MVP的模板
  • 原文地址:https://www.cnblogs.com/djoker/p/6618804.html
Copyright © 2011-2022 走看看