zoukankan      html  css  js  c++  java
  • Spring MVC异常友好展示

    官网

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

    Springmvc.xml

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

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

        <!-- 异常友好展示 -->
        <bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
            <!-- 默认配置,没有配置的异常或者httpcode返回500,指定异常为ex,前端可以用${ex.message}显示异常信息--> 
            <property name="defaultErrorView" value="errorpages/500"/>
            <property name="defaultStatusCode" value="500"/>
            <property name="exceptionAttribute" value="ex"/>
            <property name="warnLogCategory" value="WARN"/>
            <!-- Exception对应的jsp -->
            <property name="exceptionMappings">
               <props>
                  <!-- java.lang.RunTimeException异常返回errorpages/500.jsp页面,其他同理 -->
                  <prop key="java.lang.RunTimeException">errorpages/500</prop> 
                  <prop key="java.sql.SQLException">errorpages/500</prop>
                  <prop key="org.springframework.web.multipart.MaxUploadSizeExceededException">errorpages/upLoadFileError</prop>  
               </props>
            </property>
            <!-- code对应的jsp -->
            <property name="statusCodes">
               <props>
                     <!-- 404对应errorpages/404.jsp -->
                     <prop key="errorpages/404">404</prop>
                     <prop key="errorpages/404">400</prop>
               </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;auto;height:auto;margin-top: 180px;">
                  您请求的地址不存在,浏览器将在&nbsp;
                  <font color="red"><span id="txt">10</span></font>&nbsp;秒后返回.&nbsp;&nbsp;
                  <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>404</error-code>
             <location>/errorpages/404.jsp</location>
         </error-page>
        
         <error-page>
             <error-code>400</error-code>
             <location>/errorpages/400.jsp</location>
         </error-page>
  • 相关阅读:
    git常用指令 github版本回退 reset
    三门问题 概率论
    如何高效的学习高等数学
    数据库6 关系代数(relational algebra) 函数依赖(functional dependency)
    数据库5 索引 动态哈希(Dynamic Hashing)
    数据库4 3层结构(Three Level Architecture) DBA DML DDL DCL DQL
    梦想开始的地方
    java String字符串转对象实体类
    java 生成图片验证码
    java 对象之间相同属性进行赋值
  • 原文地址:https://www.cnblogs.com/aeolian/p/11970521.html
Copyright © 2011-2022 走看看