Spring ExceptionHandlerResolver Example

转载: http://forum.springsource.org/archive/index.php/t-9726.html

 

目前,如果用Spring 2.5.5 和 Tomcat 5.5+. Spring 2.5.5 有一个Bug,会导致你导向的错误页面 被Tomcat HTTP STATUS 500 internal error() 页面所替代.  https://jira.springframework.org/browse/SPR-4973

解决办法是: 升级Spring 2.5.5 到 2.5.6 (包括Spring-MVC.jar)

 

The following example uses a dummy Controller that throws unchecked Exceptions (ArrayStoreException, ArithmeticException) depending on the value of the parameter "class"

1. SpringController:

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.mvc.Controller;
import org.springframework.web.servlet.ModelAndView;

public class SpringController implements Controller {

public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {

if ("arraystore".equals(request.getParameter("class")))
throw new ArrayStoreException();
else
throw new ArithmeticException();
}
}


2. ArrayStoreExceptionHandler handles ArrayStoreException:

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.core.Ordered;
import org.springframework.web.servlet.HandlerExceptionRe solver;
import org.springframework.web.servlet.ModelAndView;


public class ArrayStoreExceptionHandler implements HandlerExceptionResolver, Ordered {
private int order;

public ModelAndView resolveException(HttpServletRequest request,
HttpServletResponse response,
Object handler,
Exception e) {
if (e instanceof ArrayStoreException)
return new ModelAndView("arraystoreView");
else
return null;
}

public int getOrder() {
return order;
}

public void setOrder(int order) {
this.order = order;
}
}


3. ArithmeticExceptionHandler (very simillar) handles ArithmeticException:

...
if (e instanceof ArithmeticException)
return new ModelAndView("arithmeticView");
else
return null;
...


4. spring-servlet.xml putting it all together:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
<bean id="springController" class="SpringController"/>

<bean id="handler1" class="ArithmeticExceptionHandler">
<property name="order"><value>1</value></property>
</bean>

<bean id="handler2" class="ArrayStoreExceptionHandler">
<property name="order"><value>2</value></property>
</bean>

<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlH andlerMapping">
<property name="mappings">
<props>
<prop key="/exception.htm">springController</prop>
</props>
</property>
</bean>

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResou rceViewResolver">
<property name="viewClass">
<value>org.springframework.web.servlet.view.InternalResou rceView</value>
</property>
<property name="prefix"><value>/</value></property>
<property name="suffix"><value>.html</value></property>
</bean>
</beans>


5. web.xml:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>

<display-name>Forums</display-name>
<description>Forums demo web application</description>

<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>

<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>


6. index.html:

<html>
<head>
<title>Exception Handler</title>
</head>
<body>
<a href="exception.htm?class=arraystore">ArrayStoreExceptionHandler</a><br>
<a href="exception.htm?class=arithmetic">ArithmeticExceptionHandler</a>
</body>
</html>


7. arraystoreView.html:

<html>
<head>
<title>Exception Handler</title>
</head>
<body>
ArrayStoreExceptionHandler called!!!
</body>
</html>


7. arithmeticView.html:

<html>
<head>
<title>Exception Handler</title>
</head>
<body>
ArithmeticExceptionHandler called!!!
</body>
</html>


8. How does it work?
When DispatchServlet is instanciated by the web container, it calls

private void initHandlerExceptionResolvers() throws BeansException {
this.handlerExceptionResolvers = new ArrayList();
if (this.detectAllHandlerExceptionResolvers) {
// find all HandlerExceptionResolvers in the ApplicationContext
Map matchingBeans = BeanFactoryUtils.beansOfTypeIncludingAncestors(
getWebApplicationContext(), HandlerExceptionResolver.class, true, false);
this.handlerExceptionResolvers = new ArrayList(matchingBeans.values());
// we keep HandlerExceptionResolvers in sorted order
Collections.sort(this.handlerExceptionResolvers, new OrderComparator());
}
else {
try {
Object her = getWebApplicationContext().getBean(HANDLER_EXCEPTI ON_RESOLVER_BEAN_NAME);
this.handlerExceptionResolvers.add(her);
}
catch (NoSuchBeanDefinitionException ex) {
// ignore, no HandlerExceptionResolver is fine too
}
}
}

DispatchServlet is then aware of ArrayStoreExceptionHandler and ArithmeticExceptionHandler and knows in whitch order they should be called.

Question
:?: I do not know if it is a good idea to paste a lot of code!! does phpB supports attachments?

 

 

PotPlayer自用皮肤 自用 PotPlayer 皮肤,仅微调了字体大小与颜色,是按个人习惯调整,未做适配性调整。 立即下载

相关推荐

2-shell及常用命令-续.pdf

2-shell及常用命令-续.pdf

HandlerExceptionResolver使用

1:介绍: 2:使用: 2.1 项目结构: 2.2:编写异常处理类: 2.3 编写控制器: 2.4:编写异常页面 2.5:测试: 1:介绍: 我们使用SimpleMappingExceptionResolver获取不到异常对象,为了能够简化代码,而且能够获取到异常对象,我们使用HandlerExceptionResolver来处理异常,并进行页面的跳转。 2:使用: 2....

刘刘刘刘刘牛皮的博客 3681

MATLAB实战:手把手教你搭建构网逆变器同步稳定性仿真模型(附避坑指南)

本文详细介绍了如何在MATLAB/Simulink平台上搭建构网逆变器同步稳定性仿真模型,涵盖从基础模块配置到虚拟同步机控制实现的完整流程。针对电力电子工程师常见的控制参数整定困难、仿真结果偏差等问题,提供了LCL滤波器谐振抑制、虚拟惯量优化等关键技术解决方案,并附有避坑指南和工程实践技巧,助力研究者高效完成仿真建模。

weixin_29051193的博客 297

使用HandlerExceptionResolver处理spring异常

文章目录前言一、如何处理异常?二、解决异常2.1 使用自定义方式2.1.1 TestException2.1.2 springmvc.xml2.2 使用spring内部的异常处理器 前言 使用spring解决异常的方式。 一、如何处理异常? 在spring中,处理异常的方式除了使用try catch自己处理的方式以外,任何模块出现异常,可以直接将异常一直throws的方式,抛给spring容器中的前端控制器,再集中对异常进行处理。 前端控制器使用HandlerExceptionResolver接口处

weixin_45118229的博客 1537

深入浅出ASAP2标准:手把手教你编写和解析A2L文件(含Python示例代码)

本文深入解析ASAP2标准下的A2L文件,涵盖基础概念、文件结构及Python实现代码,帮助开发者掌握汽车ECU标定和测量中的关键数据管理技术。通过详细示例,展示如何编写和解析A2L文件,提升电控数据管理效率。

happy2的博客 1100

AntV G6边动画实战:手把手教你实现数据流向箭头与虚线流动效果

本文详细解析了如何使用AntV G6实现数据流向箭头与虚线流动效果,通过自定义边动画提升数据可视化的动态表现力。从基础环境搭建到核心概念讲解,再到虚线流动动画和箭头路径动画的具体实现与参数调控,帮助开发者掌握G6边动画的高级技巧,适用于网络流量监控、数据传递等场景。

weixin_28487433的博客 265

Hibernate 2nd-level cache: 基础概念

 Introduction The goal for this document is to mainly describe the Hibernate 2nd-level caching strategy, learning how to use it and how can we got the performance benefit from the caching strategy. ...

DGenerationX的专栏 634

别再对着数据手册发愁了!用HFSS 2023 R2搞定ESP32-C3板载天线的阻抗匹配

本文详细介绍了如何利用HFSS 2023 R2进行ESP32-C3板载天线的阻抗匹配优化,解决实际PCB设计中常见的性能偏差问题。通过智能模型导入、科学参数扫描和量化优化流程,显著提升天线性能,适用于WIFI和蓝牙应用场景。

weixin_42523543的博客 447

JSTL - <form:select> 的用法

转载自: http://www.web-tag.net/3c98ca7b0cac8312.htm 下拉框组件标签       下拉框标签的典型形式为:    &lt;form: select path="city" items="${cityList}"/&gt;,它包括两方面的数据:     1)对应表单对象属性值(city);    2)用于构造整个下拉框选项的数据(cityList)...

DGenerationX的专栏 502

ARM和FPGA之间的通信(硬件原理图篇)

设计该开发板的初衷是为了学习,但也希望今后能扩展其功能,看到大家都在diy示波器,自己也非常冲动的想DIY一把,于是呼选了arm+fpga的方案,fpga主要用来实现数据的高速采样,而ARM则提供数据的显示,以及一些人机交互的功能。那么该方案中最重要的地方就是ARM和FPGA之间的通信如何实现了。在网上查了很多的资料,有说将FPGA作为ARM的一个外设,可以通过DMA的方式来直接访问;也有说把FP

屌yy的博客 5万+

史上最全的Logisim存储系统实验教程(基于华中科技大学计算机组成原理实验)内赋源码下载

本博客是最全的最好的解释的华中科技大学计算机组成实验存储系统实验,本博客给出了电路实现,也给出了使用电路的原因有利于新手学习十分详细。并且内赋源码下载。

qq_62325622的博客 2万+

JUDE UML 操作技巧

1. How to create Assembly Connector - 1 Step 1: Step 2: Step 3: Completed 2. How to create Assembly Connector - 2 Step 1. There are two same interfaces on diagram. One is connected by...

DGenerationX的专栏 421

Win11 1 月更新黑屏循环故障技术分析与解决方案

2026年1月30日Windows 11更新引发黑屏循环故障,主要影响2018年前出厂、采用MBR分区及特定BIOS固件的设备,尤其对使用经典版Outlook(POP协议)的用户影响显著。故障表现为系统无限重启黑屏,导致数据访问中断。分析表明,问题源于硬件兼容性、分区格式与系统更新的交互冲突。解决方案包括进入安全模式回滚更新、暂停自动更新、升级驱动/BIOS,以及等待微软官方补丁。建议用户更新前评估硬件兼容性并备份数据,企业环境应建立分阶段更新测试机制。该事件为系统更新风险管理提供了重要参考。

博客 1441

Java Thread: CountDownLatch 例子

public class CountDownLatchTest {    @Test  public void ThreadControllTest() throws InterruptedException{   // Suppose we have total 10 thread waiting to execute   final int TOTAL_THREAD_NUM = 10...

DGenerationX的专栏 352

粒子群优化与蚁群算法求解多旅行商问题

它要求在给定一组城市和旅行商之间的距离矩阵的情况下,找到一条最优的路径方案,使得所有旅行商可以分别从起始城市出发,途径其他城市,并最终回到起始城市,同时总旅行距离达到最小。这两种算法都是基于群体智能的优化算法,在求解组合优化问题时具有较好的性能表现。假设有n个城市需要被m个旅行商访问,城市间的距离由距离矩阵D表示,其中D(i,j)表示第i个城市到第j个城市的距离。粒子群优化算法是一种基于群体智能的优化算法,其中的粒子代表了潜在的解,并通过模拟粒子在解空间中的移动来搜索最优解。一、多旅行商问题建模。

m0_47037246的博客 553

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

Cruise Control 持续集成 - 如何自动编译,打包,并自动发布至Tomcat

一, 前言 目标是,利用CruiseControl 自动抓取SVN下的源代码,并且将项目源码自动编译,打包成war,并自动部署到 Tomcat 中。   二, CruiseControl 如何自动获取SVN源码 1. 首先, 将 SVN 工程 checkout 到 CruiseControl Project 目录下。   cd  ${CC_PROJECT_PATH}/your...

DGenerationX的专栏 350

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

eclipse indigo 目前运行时遇到的问题汇总

用了几天 Eclipse Indigo, 运行时经常无故退出。   1. 启动报错,can not initialize JVM     解决办法: 修改 eclipse.ini,将 XXMaxPermSize 改为 128M   2. 用 SVN Checkout 项目,经常报错,无故退出     这个问题让我百思不得其解,但是解决办法是,将我本地安装的 SVN Server 端 给...

DGenerationX的专栏 347

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

Java 如何获得本机IP 或者 公网IP

你应该是拔号上网 如果可以的话,请搜索一下网络,得到java怎么调用dos的方法,然后你可以解析ipconfig /all这个命令的结果,得到你想要的公网IP 大部分情况下,用户都是通过路由来上网的,也就是说,电脑根本不知道自己的公网IP是多少,他只知道路由分配给它的内网IP,这个时候,我们只能通过别的网站来判断。 下面是以前我以前写的一个小程序,通过www.ip138.com 得到自...

DGenerationX的专栏 343

Hibernate 2nd-level cache: JBoss Caching 配置与注意事项

依赖包: jboss-cache-1.4.1.jar jboss-client-4.0.2.jar jboss-common-client.jar jboss-jmx.ja jboss-system-4.0.2.jar jgroups-2.4.1.jar concurrence   check those under the attached file ensential-jar...

DGenerationX的专栏 335

Sysdeo Eclipse Tomcat Launcher plugin 使用总结

TomcatLauncher plugin 很轻量级,很喜欢. 以下是如何通过 TomcatLauncher 搭建Tomcat 开发环境 step-by-step   1. 工程目录:         注 1. conf/server.xml 直接拷贝自 Tomcat 安装目录下的conf 文件,唯一要修改的地方是        注 2. WEB-INF 下面还应创建一个 classes ...

DGenerationX的专栏 295

Hiberante 中 sort 的实现方式

http://hi.baidu.com/accpandsvse/blog/item/a12c22f23d0d1bc20a46e091.html http://hi.baidu.com/accpandsvse/blog/item/a12c22f23d0d1bc20a46e091.html 從資料庫的觀點來看,Set、Map、Bag是無序的,而List是有序的,這邊所謂的無序或有序,是指將容器...

DGenerationX的专栏 259

Java 打包成可执行程序 .exe

将java应用程序打包成独立运行的.exe方法 2009年02月06日 星期五 22:10 将java应用程序打包成独立运行的.exe方法 对于windows用户来说,将java应用程序打包成.exe最好不过了,现在我介绍两个方法。 1、用专业的应用程序打包工具InstallAnywhere,这个软件几乎能在所有平台上运行,当然你要下载到你需要的那个平台的啦!而且它也...

DGenerationX的专栏 251

JAXB Marchel and UnMarchel Quick Start

reference link: http://www.oracle.com/technetwork/articles/javase/index-140168.html (1.x) http://www.java-tips.org/java-ee-tips/java-architecture-for-xml-binding/what-is-new-in-jaxb-2.0.html   Plu...

DGenerationX的专栏 240

操作 Cookie

Cookie包含的属性   名称, 内容, 主机, 路径 ....    Cookie的唯一性   一个唯一的Cookie 由 名称,主机,路径 确定.  这里路径是最容易被忽略的,如下图,这两个Cookie虽然名称,主机名都相同,但是确实两个不同的Cookie.   1) 有两个同名的 (PRODUCTS_IN_COOKIE)   2) 两个同名的Cookie 唯一...

DGenerationX的专栏 233

What is J2EE ( in just one simple sentence )

What is J2EE?   J2EE is essentially about standardizing a range of enterprise services, such as naming and directory services (JNDI), transaction management offering a standard API potentially spa...

DGenerationX的专栏 225
上一篇: Userfull Links
下一篇: Hibernate 2nd-level cache: 基础概念
伤神v
博客等级 码龄19年 17粉丝 129原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值