本人微信公众号"aeolian"~

Spring MVC异常友好展示

官网

https://docs.spring.io/spring/docs/4.3.25.RELEASE/spring-framework-reference/htmlsingle/

《Spring MVC异常友好展示》

Springmvc.xml

配置springmvc.xml,出现exception返回的界面和对应code返回的界面。

不知道为啥httpcode不起作用,网上也没查到相关资料,于是httpcode对应的异常只能用web.xml实现。

    
    <bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
         
        <property name="defaultErrorView" value="errorpages/500"/>
        <property name="defaultStatusCode" value="500"/>
        <property name="exceptionAttribute" value="ex"/>
        <property name="warnLogCategory" value="WARN"/>
        
        <property name="exceptionMappings">
           <props>
              
              <prop key="java.lang.RunTimeException">errorpages/500prop> 
              <prop key="java.sql.SQLException">errorpages/500prop>
              <prop key="org.springframework.web.multipart.MaxUploadSizeExceededException">errorpages/upLoadFileErrorprop>  
           props>
        property>
        
        <property name="statusCodes">
           <props>
                 
                 <prop key="errorpages/404">404prop>
                 <prop key="errorpages/404">400prop>
           props>
        property>
    bean>

/errorpages/404.jsp

显示一段时间后自动返回.

<%@ page language="java"  pageEncoding="utf-8"%>
DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>error(404)title>
    <meta charset="utf-8"/>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">
    <script type="text/javascript">
        var m=9;
        function init(){
            var t=document.getElementById("txt");
            interval=window.setInterval(function(){
                 t.innerText=m+"";
                 if(m<=0){
                     window.clearInterval(interval);
                     window.history.go(-1);
                 }
                 m--;
            }, 1000);
        }
    script>
  head>
  <body onload="init();">
      <div style="vertical-align: center;text-align: center;width:auto;height:auto;margin-top: 180px;">
              您请求的地址不存在,浏览器将在 
              <font color="red"><span id="txt">10span>font> 秒后返回.  
              <a href="javascript:window.history.go(-1);">立即返回a>
      div>
  body>
html>

/errorpages/500.jsp

显示报错

<%@ page language="java"  pageEncoding="utf-8"%>
DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>请求错误(500)title>
    <meta charset="utf-8"/>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">
    <script type="text/javascript">
    script>
  head>
  <body>
     <div>
          请求错误,错误信息如下:${ex.message}
     div>
  body>
html>

web.xml

配置httpcode

     <error-page>
         <error-code>404error-code>
         <location>/errorpages/404.jsplocation>
     error-page>
    
     <error-page>
         <error-code>400error-code>
         <location>/errorpages/400.jsplocation>
     error-page>
点赞

Leave a Reply

Your email address will not be published. Required fields are marked *