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?

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值