1.在spring mvc下实现统一异常处理很方便,只要在web.xml中配置异常时要显示的页面即可,如下:

  1. <error-page>  
  2.     <exception-type>java.lang.Exception</exception-type>  
  3.     <location>/html/500.htm</location>  
  4. </error-page>  
  5. <error-page>  
  6.     <error-code>404</error-code>  
  7.     <location>/html/404.htm</location>  
  8. </error-page>  
<error-page>
	<exception-type>java.lang.Exception</exception-type>
	<location>/html/500.htm</location>
</error-page>
<error-page>
	<error-code>404</error-code>
	<location>/html/404.htm</location>
</error-page>

指定了异常时显示的页面为/html/500.htm,在这个页面中可以显示一些友好的提示给用户,避免把出错信息暴露出去。

2.那么这个/html/500.htm的页面在项目中的地址是什么呢?

如果按照默认的配置,系统会找RequestMapping(value="/html/500")的action,但这个页面是一个静态页面,没有必要再走mvc,所以可以把html目录配置成静态文件目录来绕过mvc解析,如下配置:

  1. <beans:beans   
  2.      ........  
  3.   
  4.     <resources mapping="/html/**" location="/resources/html/" />  
  5.   
  6.     .........  
<beans:beans 
     ........

	<resources mapping="/html/**" location="/resources/html/" />

    .........

在bean文件中把/html/**的文件指到一个目录下,当成资源访问,同样的方式,还可以实现favicon.ico在根目录的访问

 

 

  1. <resources mapping="/favicon.ico" location="/resources/images/favicon.ico" />  
<resources mapping="/favicon.ico" location="/resources/images/favicon.ico" />


这样就可以了,最后附下目录结构:

 

 

转帖:http://blog.csdn.net/wuxinzaiyu/article/details/8524025

posted on 2013-05-28 13:59  竹林后生  阅读(240)  评论(0编辑  收藏  举报