diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/handler/SimpleMappingExceptionResolver.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/handler/SimpleMappingExceptionResolver.java index 915de5ff43..662f8c77c3 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/handler/SimpleMappingExceptionResolver.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/handler/SimpleMappingExceptionResolver.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2009 the original author or authors. + * Copyright 2002-2012 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -36,6 +36,7 @@ import org.springframework.web.util.WebUtils; * * @author Juergen Hoeller * @author Arjen Poutsma + * @author Rossen Stoyanchev * @since 22.11.2003 * @see org.springframework.web.servlet.DispatcherServlet */ @@ -46,6 +47,8 @@ public class SimpleMappingExceptionResolver extends AbstractHandlerExceptionReso private Properties exceptionMappings; + private Class>[] excludedExceptions; + private String defaultErrorView; private Integer defaultStatusCode; @@ -73,6 +76,16 @@ public class SimpleMappingExceptionResolver extends AbstractHandlerExceptionReso this.exceptionMappings = mappings; } + /** + * Set one or more exceptions to be excluded from the exception mappings. + * Excluded exceptions are checked first and if one of them equals the actual + * exception, the exception will remain unresolved. + * @param excludedExceptions one or more excluded exception types + */ + public void setExcludedExceptions(Class>... excludedExceptions) { + this.excludedExceptions = excludedExceptions; + } + /** * Set the name of the default error view. This view will be returned if no specific mapping was found.
Default is
* none.
@@ -98,13 +111,13 @@ public class SimpleMappingExceptionResolver extends AbstractHandlerExceptionReso
}
/**
- * An alternative to {@link #setStatusCodes(Properties)} for use with
+ * An alternative to {@link #setStatusCodes(Properties)} for use with
* Java-based configuration.
*/
public void addStatusCode(String viewName, int statusCode) {
this.statusCodes.put(viewName, statusCode);
}
-
+
/**
* Returns the HTTP status codes provided via {@link #setStatusCodes(Properties)}.
* Keys are view names; values are status codes.
@@ -172,14 +185,23 @@ public class SimpleMappingExceptionResolver extends AbstractHandlerExceptionReso
}
/**
- * Determine the view name for the given exception, searching the {@link #setExceptionMappings "exceptionMappings"},
- * using the {@link #setDefaultErrorView "defaultErrorView"} as fallback.
+ * Determine the view name for the given exception, first checking against the
+ * {@link #setExcludedExceptions(Class[]) "excludedExecptions"}, then searching the
+ * {@link #setExceptionMappings "exceptionMappings"}, and finally using the
+ * {@link #setDefaultErrorView "defaultErrorView"} as a fallback.
* @param ex the exception that got thrown during handler execution
* @param request current HTTP request (useful for obtaining metadata)
- * @return the resolved view name, or null
if none found
+ * @return the resolved view name, or null
if excluded or none found
*/
protected String determineViewName(Exception ex, HttpServletRequest request) {
String viewName = null;
+ if (this.excludedExceptions != null) {
+ for (Class> excludedEx : this.excludedExceptions) {
+ if (excludedEx.equals(ex.getClass())) {
+ return null;
+ }
+ }
+ }
// Check for specific exception mappings.
if (this.exceptionMappings != null) {
viewName = findMatchingViewName(this.exceptionMappings, ex);
diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/handler/SimpleMappingExceptionResolverTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/handler/SimpleMappingExceptionResolverTests.java
index 1be740640d..632e53054a 100644
--- a/spring-webmvc/src/test/java/org/springframework/web/servlet/handler/SimpleMappingExceptionResolverTests.java
+++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/handler/SimpleMappingExceptionResolverTests.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2002-2007 the original author or authors.
+ * Copyright 2002-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,14 +16,16 @@
package org.springframework.web.servlet.handler;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+
import java.util.Collections;
import java.util.Properties;
+
import javax.servlet.http.HttpServletResponse;
-import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
-
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
@@ -196,6 +198,16 @@ public class SimpleMappingExceptionResolverTests {
assertNull(mav);
}
+ @Test
+ public void simpleExceptionMappingWithExclusion() {
+ Properties props = new Properties();
+ props.setProperty("Exception", "error");
+ exceptionResolver.setExceptionMappings(props);
+ exceptionResolver.setExcludedExceptions(IllegalArgumentException.class);
+ ModelAndView mav = exceptionResolver.resolveException(request, response, handler1, new IllegalArgumentException());
+ assertNull(mav);
+ }
+
@Test
public void missingExceptionInMapping() {
Properties props = new Properties();
diff --git a/src/dist/changelog.txt b/src/dist/changelog.txt
index f2940ec853..857563a576 100644
--- a/src/dist/changelog.txt
+++ b/src/dist/changelog.txt
@@ -23,6 +23,7 @@ Changes in version 3.2 M1
* Prevent further writing to the response when @ResponseStatus contains a reason
* Deprecate HttpStatus codes 419, 420, 421
* support access to all URI vars via @PathVariable Map