转载: 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?