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

本文介绍了如何在SpringMVC项目中配置404和500错误页面,并通过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

转载于:https://www.cnblogs.com/tenWood/p/7535899.html

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 1. 组件扫描(根据实际包名修改) --> <context:component-scan base-package="cn.edu.uibe.controller"/> <!-- 2. 注解驱动(必须启用) --> <mvc:annotation-driven/> <!-- 3. 静态资源处理(关键修改) --> <mvc:resources mapping="/statics/**" location="/statics/" cache-period="31536000"/> <mvc:resources mapping="/images/**" location="/images/" cache-period="31536000"/> <!-- 4. 兜底静态资源处理(必须添加) --> <mvc:default-servlet-handler/> <!-- 5. 视图解析器(优化配置) --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"/> <property name="suffix" value=".jsp"/> <!-- 开发阶段建议禁用缓存 --> <property name="cache" value="false"/> </bean> <!-- 6. 文件上传(优化配置) --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize" value="10485760"/> <!-- 10MB --> <property name="defaultEncoding" value="UTF-8"/> <property name="resolveLazily" value="true"/> </bean> <!-- 7. 拦截器配置(示例,可选) --> <mvc:interceptors> <bean class="cn.edu.uibe.interceptor.AccessInterceptor"/> </mvc:interceptors> <!-- 8. 消息换器(可选) --> <mvc:message-converters> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <property name="defaultCharset" value="UTF-8"/> </bean> <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/> </mvc:message-converters> </beans>/这是spring-mvc.xml文件,给出完整的修改后的文件
最新发布
07-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值