【Java】spring MVC中定义异常页面

137 阅读4分钟
原文链接: click.aliyun.com

spring MVC中定义异常页面

泳泳啊泳泳 2018-01-08 14:10:43 浏览155 评论0

java spring web 配置 exception xml class schema bean html Servlet JSP MVC utf-8

摘要: 如果我们在使用Spring MVC的过程中,想自定义异常页面的话,我们可以使用DispatcherServlet来指定异常页面,具体的做法很简单: 下面看我曾经的一个项目的spring配置文件: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 <?xml version="1.

如果我们在使用Spring MVC的过程中,想自定义异常页面的话,我们可以使用DispatcherServlet来指定异常页面,具体的做法很简单:

下面看我曾经的一个项目的spring配置文件:




<?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:p="http://www.springframework.org/schema/p"


       xmlns:context="http://www.springframework.org/schema/context"


       xsi:schemaLocation="http://www.springframework.org/schema/beans


       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd


       http://www.springframework.org/schema/context


       http://www.springframework.org/schema/context/spring-context-3.0.xsd">


    <!-- 扫描web包,应用Spring的注解 -->


    <context:component-scan base-package="com.xxx.training.spring.mvc"/>


 



    <!-- 配置视图解析器,将ModelAndView及字符串解析为具体的页面 -->




    <bean




            class="org.springframework.web.servlet.view.InternalResourceViewResolver"




            p:viewClass="org.springframework.web.servlet.view.JstlView"




            p:prefix="/WEB-INF/views/"




            p:suffix=".jsp"/>



 



    <!--定义异常处理页面-->




    <bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">




        <property name="exceptionMappings">




            <props>




                <prop key="java.sql.SQLException">outException</prop>




                <prop key="java.io.IOException">outException</prop>




            </props>




        </property>




    </bean>




</beans>



  上面的定义异常处理部分的解释为:只要发生了SQLException或者IOException异常,就会自动跳转到WEB-INF/views/outException.jsp页面。

一般情况下我们的outException.jsp页面的代码为:




<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<html>
<head>

    <title>异常处理页面</title>

</head>
<body>


<% Exception ex = (Exception) request.getAttribute("Exception");%>



<H2>Exception:<%=ex.getMessage()%>


</H2>


</body>


</html>


  当然你也可以修改样式,这个就看个人喜好了、

另外记得要在web.xml也使用类似下面的方式处理异常哦。:




<error-page>


     <error-code>404</error-code>


     <location>/WEB-INF/pages/404.jsp</location>



 </error-page>



 



 <error-page>




     <exception-type>java.lang.Exception</exception-type>




     <location>/WEB-INF/pages/exception.jsp</location>




 </error-page>



  因为这两个异常处理的维度是不一样的,简单说,spring的resolver是spring内部使用的,而web。xml里的是整个webapp共同使用的。

建议两个都配置上,

因为spring的resolver可以和spring结合的更紧密,可扩展的更多。


============================================================================== 本文转自被遗忘的博客园博客,原文链接:http://www.cnblogs.com/rollenholt/archive/2012/12/25/2832731.html,如需转载请自行联系原作者
版权声明:本文内容由互联网用户自发贡献,版权归作者所有,本社区不拥有所有权,也不承担相关法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件至: yqgroup@service.aliyun.com 进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容。

用云栖社区APP,舒服~

【云栖快讯】中办国办印发《推进互联网协议第六版(IPv6)规模部署行动计划》加快推进基于 IPv6 的下一代互联网规模部署,计划指出2025年末中国 IPv6 规模要达到世界第一,阿里云也第一时间宣布了将全面提供IPv6服务,那么在全面部署 IPV6 前,你需要了解都在这儿  详情请点击 评论文章 (0) (0) (0)

相关文章

网友评论