Java教程

Spring MVC异常友好展示

本文主要是介绍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 onl oad="init();">
      <div style="vertical-align: center;text-align: center;width: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>
这篇关于Spring MVC异常友好展示的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!