转转转![Spring MVC] - 500/404错误处理-SimpleMappingExceptionResolver

Posted 有点懒惰的大青年

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了转转转![Spring MVC] - 500/404错误处理-SimpleMappingExceptionResolver相关的知识,希望对你有一定的参考价值。

参考博客:

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

以上是关于转转转![Spring MVC] - 500/404错误处理-SimpleMappingExceptionResolver的主要内容,如果未能解决你的问题,请参考以下文章

Spring mvc关于html中from action跳转问题

https怎么强制转成http

WS转WSS协议

YouTUBE上的视频怎么转哉

VMWARE怎么改BIOS(转)

Spring MVC中用@ResponseBody转json,对json进行处理方法汇总