• 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> 
  • 相关阅读:
    Linux添加用户组和删除用户组
    购物意图分析
    架构是什么来的
    如何突破浏览器加载并发数的限制
    写JS自执行函数时要注意的
    网页是什么
    JVM
    javascript的边界
    浏览器
    HTTP
  • 原文地址:https://www.cnblogs.com/djoker/p/6618804.html
Copyright © 2020-2023  润新知