• 转转转![Spring MVC]


    参考博客:

    http://www.cnblogs.com/dongying/p/6129937.html

    http://www.cnblogs.com/rollenholt/archive/2012/12/25/2832731.html

    http://cgs1999.iteye.com/blog/1547197

    我在项目中的使用:

    1)404找不到:

    web.xml中配置:

    <!-- 404错误 -->
        <error-page>
           <error-code>404</error-code>
           <location>/WEB-INF/view/error/404.jsp</location>
        </error-page>
    View Code

    2)其他的异常,在springMVC中.xml配置:

    <!-- 将Controller抛出的异常转到特定View, 保持SiteMesh的装饰效果 -->
        <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">    
            <property name="exceptionMappings">    
                <props>    
                    <prop key="java.lang.Throwable">error/myException</prop>  
                </props>    
            </property>    
             <!-- 默认错误页面,当找不到上面mappings中指定的异常对应视图时,使用本默认配置 -->
            <property name="defaultErrorView" value="error/myException"></property>
            <!-- 默认HTTP状态码 -->
            <property name="defaultStatusCode" value="500"></property>
        </bean> 
    View Code

    3)对应的异常页面:

    myException.jsp:

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" isErrorPage="true" %>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
    <%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
    <%
        response.setStatus(200);
    %>
    <!DOCTYPE html>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <link rel="stylesheet" type="text/css" href="resources/bootstrap/bootstrap.min.css">
        <title><spring:message code="exception"/></title>
    </head>
    
    <body>
        <h2><spring:message code="exception"/></h2>
    </body>
    <script src="resources/js/jquery.min.js"></script>
    <script src="resources/bootstrap/bootstrap.min.js"></script>
    </html>
    View Code

    404.jsp:

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" isErrorPage="true" %>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
    <%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
    <%
        response.setStatus(200);
    %>
    <!DOCTYPE html>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <link rel="stylesheet" type="text/css" href="resources/bootstrap/bootstrap.min.css">
        <title><spring:message code="code404"/></title>
    </head>
    
    <body>
        <h5><spring:message code="code404"/></h5>
    </body>
    <script src="resources/js/jquery.min.js"></script>
    <script src="resources/bootstrap/bootstrap.min.js"></script>
    </html>
    View Code

    ======================================================转载文章==========================================================================

    Spring MVC中404 找不到页面错误可以直接使用web.xml中配置:

    在<web-app/>节点内加入:

        <error-page>
            <error-code>404</error-code>
            <location>/WEB-INF/views/errors/404.jsp</location>
        </error-page>

    500的运行时错误,可以使用Spring MVC的SimpleMappingExceptionResolver配置:

    <!-- 全局异常配置 -->
        <bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
            <property name="exceptionMappings">
                <props>
                    <prop key="java.lang.Exception">errors/500</prop>
                    <prop key="java.lang.Throwable">errors/500</prop>
                </props>
            </property>
            <property name="statusCodes">
                <props>
                    <prop key="errors/500">500</prop>
                </props>
            </property>
            <!-- 设置日志输出级别,不定义则默认不输出警告等错误日志信息 -->
            <property name="warnLogCategory" value="WARN"></property>
            <!-- 默认错误页面,当找不到上面mappings中指定的异常对应视图时,使用本默认配置 -->
            <property name="defaultErrorView" value="errors/500"></property>
            <!-- 默认HTTP状态码 -->
            <property name="defaultStatusCode" value="500"></property>
        </bean>

    对应500错误的view jsp页面:

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
    <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
    <!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=UTF-8">
    <title>500 Error</title>
    </head>
    <body>
        <% Exception ex = (Exception)request.getAttribute("exception"); %>
        <H2>Exception: <%= ex.getMessage()%></H2>
        <P/>
        <% ex.printStackTrace(new java.io.PrintWriter(out)); %>
    </body>
    </html>

    测试:

    另外,也可以使用继承HandlerExceptionResolver来处理500的错误。

    参考文章引用:

    http://www.cnblogs.com/xguo/p/3163519.html

  • 相关阅读:
    Hadoop 2.2.0单节点的伪分布集成环境搭建
    CentOS系统中出现错误--SSH:connect to host centos-py port 22: Connection refused
    JUnit 5和Selenium基础(三)
    JUnit 5和Selenium基础(二)
    JUnit 5和Selenium基础(一)
    如何对N个接口按比例压测
    性能测试中标记每个请求
    初学者的API测试技巧
    Web安全检查
    Groovy重载操作符
  • 原文地址:https://www.cnblogs.com/tenWood/p/7535899.html
Copyright © 2020-2023  润新知