diff --git a/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/ModelAndView.java b/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/ModelAndView.java index f4d4a6742ba..e044b4bc217 100644 --- a/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/ModelAndView.java +++ b/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/ModelAndView.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2009 the original author or authors. + * Copyright 2002-2010 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. @@ -49,9 +49,7 @@ public class ModelAndView { /** Model Map */ private ModelMap model; - /** - * Indicates whether or not this instance has been cleared with a call to {@link #clear()}. - */ + /** Indicates whether or not this instance has been cleared with a call to {@link #clear()} */ private boolean cleared = false; @@ -302,4 +300,5 @@ public class ModelAndView { sb.append("; model is ").append(this.model); return sb.toString(); } + } diff --git a/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/annotation/AnnotationMethodHandlerAdapter.java b/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/annotation/AnnotationMethodHandlerAdapter.java index d2dc8aed336..e610cd4b15d 100644 --- a/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/annotation/AnnotationMethodHandlerAdapter.java +++ b/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/annotation/AnnotationMethodHandlerAdapter.java @@ -847,8 +847,8 @@ public class AnnotationMethodHandlerAdapter extends WebContentGenerator // Invoke custom resolvers if present... if (customModelAndViewResolvers != null) { for (ModelAndViewResolver mavResolver : customModelAndViewResolvers) { - ModelAndView mav = mavResolver - .resolveModelAndView(handlerMethod, handlerType, returnValue, implicitModel, webRequest); + ModelAndView mav = mavResolver.resolveModelAndView( + handlerMethod, handlerType, returnValue, implicitModel, webRequest); if (mav != ModelAndViewResolver.UNRESOLVED) { return mav; } diff --git a/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/annotation/ResponseStatusExceptionResolver.java b/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/annotation/ResponseStatusExceptionResolver.java index 3419f30594b..3c0a43f211a 100644 --- a/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/annotation/ResponseStatusExceptionResolver.java +++ b/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/annotation/ResponseStatusExceptionResolver.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2009 the original author or authors. + * Copyright 2002-2010 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. @@ -28,6 +28,7 @@ import org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver; /** * Implementation of the {@link org.springframework.web.servlet.HandlerExceptionResolver HandlerExceptionResolver} * interface that uses the {@link ResponseStatus @ResponseStatus} annotation to map exceptions to HTTP status codes. + * *

This exception resolver is enabled by default in the {@link org.springframework.web.servlet.DispatcherServlet}. * * @author Arjen Poutsma @@ -36,10 +37,9 @@ import org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver; public class ResponseStatusExceptionResolver extends AbstractHandlerExceptionResolver { @Override - protected ModelAndView doResolveException(HttpServletRequest request, - HttpServletResponse response, - Object handler, - Exception ex) { + protected ModelAndView doResolveException(HttpServletRequest request, HttpServletResponse response, + Object handler, Exception ex) { + ResponseStatus responseStatus = AnnotationUtils.findAnnotation(ex.getClass(), ResponseStatus.class); if (responseStatus != null) { try { @@ -56,20 +56,17 @@ public class ResponseStatusExceptionResolver extends AbstractHandlerExceptionRes * Template method that handles {@link ResponseStatus @ResponseStatus} annotation.

Default implementation send a * response error using {@link HttpServletResponse#sendError(int)}, or {@link HttpServletResponse#sendError(int, * String)} if the annotation has a {@linkplain ResponseStatus#reason() reason}. Returns an empty ModelAndView. - * * @param responseStatus the annotation * @param request current HTTP request * @param response current HTTP response - * @param handler the executed handler, or null if none chosen at the time of the exception (for example, - * if multipart resolution failed) + * @param handler the executed handler, or null if none chosen at the time of the exception + * (for example, if multipart resolution failed) * @param ex the exception that got thrown during handler execution * @return a corresponding ModelAndView to forward to, or null for default processing */ - protected ModelAndView resolveResponseStatus(ResponseStatus responseStatus, - HttpServletRequest request, - HttpServletResponse response, - Object handler, - Exception ex) throws Exception { + protected ModelAndView resolveResponseStatus(ResponseStatus responseStatus, HttpServletRequest request, + HttpServletResponse response, Object handler, Exception ex) throws Exception { + int statusCode = responseStatus.value().value(); String reason = responseStatus.reason(); if (!StringUtils.hasLength(reason)) { @@ -80,4 +77,5 @@ public class ResponseStatusExceptionResolver extends AbstractHandlerExceptionRes } return new ModelAndView(); } + } diff --git a/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/support/DefaultHandlerExceptionResolver.java b/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/support/DefaultHandlerExceptionResolver.java index b2c5f06d94b..c33907d0351 100644 --- a/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/support/DefaultHandlerExceptionResolver.java +++ b/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/support/DefaultHandlerExceptionResolver.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2009 the original author or authors. + * Copyright 2002-2010 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. @@ -31,9 +31,9 @@ import org.springframework.http.converter.HttpMessageNotReadableException; import org.springframework.http.converter.HttpMessageNotWritableException; import org.springframework.util.CollectionUtils; import org.springframework.util.StringUtils; +import org.springframework.web.HttpMediaTypeNotAcceptableException; import org.springframework.web.HttpMediaTypeNotSupportedException; import org.springframework.web.HttpRequestMethodNotSupportedException; -import org.springframework.web.HttpMediaTypeNotAcceptableException; import org.springframework.web.bind.MissingServletRequestParameterException; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver; @@ -41,11 +41,13 @@ import org.springframework.web.servlet.mvc.multiaction.NoSuchRequestHandlingMeth /** * Default implementation of the {@link org.springframework.web.servlet.HandlerExceptionResolver - * HandlerExceptionResolver} interface that resolves standard Spring exceptions.

Default implementations typically - * set the response status to a corresponding HTTP status code.

This exception resolver is enabled by default in the - * {@link org.springframework.web.servlet.DispatcherServlet}. + * HandlerExceptionResolver} interface that resolves standard Spring exceptions and translates + * them to corresponding HTTP status codes. + * + *

This exception resolver is enabled by default in the {@link org.springframework.web.servlet.DispatcherServlet}. * * @author Arjen Poutsma + * @since 3.0 * @see #handleNoSuchRequestHandlingMethod * @see #handleHttpRequestMethodNotSupported * @see #handleHttpMediaTypeNotSupported @@ -53,34 +55,34 @@ import org.springframework.web.servlet.mvc.multiaction.NoSuchRequestHandlingMeth * @see #handleTypeMismatch * @see #handleHttpMessageNotReadable * @see #handleHttpMessageNotWritable - * @since 3.0 */ public class DefaultHandlerExceptionResolver extends AbstractHandlerExceptionResolver { /** * Log category to use when no mapped handler is found for a request. - * * @see #pageNotFoundLogger */ public static final String PAGE_NOT_FOUND_LOG_CATEGORY = "org.springframework.web.servlet.PageNotFound"; /** * Additional logger to use when no mapped handler is found for a request. - * * @see #PAGE_NOT_FOUND_LOG_CATEGORY */ protected static final Log pageNotFoundLogger = LogFactory.getLog(PAGE_NOT_FOUND_LOG_CATEGORY); - /** Sets the {@linkplain #setOrder(int) order} to {@link #LOWEST_PRECEDENCE}. */ + + /** + * Sets the {@linkplain #setOrder(int) order} to {@link #LOWEST_PRECEDENCE}. + */ public DefaultHandlerExceptionResolver() { setOrder(Ordered.LOWEST_PRECEDENCE); } + @Override - protected ModelAndView doResolveException(HttpServletRequest request, - HttpServletResponse response, - Object handler, - Exception ex) { + protected ModelAndView doResolveException(HttpServletRequest request, HttpServletResponse response, + Object handler, Exception ex) { + try { if (ex instanceof NoSuchRequestHandlingMethodException) { return handleNoSuchRequestHandlingMethod((NoSuchRequestHandlingMethodException) ex, request, response, @@ -122,22 +124,20 @@ public class DefaultHandlerExceptionResolver extends AbstractHandlerExceptionRes } /** - * Handle the case where no request handler method was found.

The default implementation logs a warning, sends an - * HTTP 404 error, and returns an empty {@code ModelAndView}. Alternatively, a fallback view could be chosen, or the - * NoSuchRequestHandlingMethodException could be rethrown as-is. - * + * Handle the case where no request handler method was found. + *

The default implementation logs a warning, sends an HTTP 404 error, and returns + * an empty {@code ModelAndView}. Alternatively, a fallback view could be chosen, + * or the NoSuchRequestHandlingMethodException could be rethrown as-is. * @param ex the NoSuchRequestHandlingMethodException to be handled * @param request current HTTP request * @param response current HTTP response - * @param handler the executed handler, or null if none chosen at the time of the exception (for example, - * if multipart resolution failed) + * @param handler the executed handler, or null if none chosen + * at the time of the exception (for example, if multipart resolution failed) * @return a ModelAndView to render, or null if handled directly * @throws Exception an Exception that should be thrown as result of the servlet request */ protected ModelAndView handleNoSuchRequestHandlingMethod(NoSuchRequestHandlingMethodException ex, - HttpServletRequest request, - HttpServletResponse response, - Object handler) throws Exception { + HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { pageNotFoundLogger.warn(ex.getMessage()); response.sendError(HttpServletResponse.SC_NOT_FOUND); @@ -145,23 +145,20 @@ public class DefaultHandlerExceptionResolver extends AbstractHandlerExceptionRes } /** - * Handle the case where no request handler method was found for the particular HTTP request method.

The default - * implementation logs a warning, sends an HTTP 405 error, sets the "Allow" header, and returns an empty {@code - * ModelAndView}. Alternatively, a fallback view could be chosen, or the HttpRequestMethodNotSupportedException could - * be rethrown as-is. - * + * Handle the case where no request handler method was found for the particular HTTP request method. + *

The default implementation logs a warning, sends an HTTP 405 error, sets the "Allow" header, + * and returns an empty {@code ModelAndView}. Alternatively, a fallback view could be chosen, + * or the HttpRequestMethodNotSupportedException could be rethrown as-is. * @param ex the HttpRequestMethodNotSupportedException to be handled * @param request current HTTP request * @param response current HTTP response - * @param handler the executed handler, or null if none chosen at the time of the exception (for example, - * if multipart resolution failed) + * @param handler the executed handler, or null if none chosen + * at the time of the exception (for example, if multipart resolution failed) * @return a ModelAndView to render, or null if handled directly * @throws Exception an Exception that should be thrown as result of the servlet request */ protected ModelAndView handleHttpRequestMethodNotSupported(HttpRequestMethodNotSupportedException ex, - HttpServletRequest request, - HttpServletResponse response, - Object handler) throws Exception { + HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { pageNotFoundLogger.warn(ex.getMessage()); String[] supportedMethods = ex.getSupportedMethods(); @@ -174,22 +171,19 @@ public class DefaultHandlerExceptionResolver extends AbstractHandlerExceptionRes /** * Handle the case where no {@linkplain org.springframework.http.converter.HttpMessageConverter message converters} - * were found for the PUT or POSTed content.

The default implementation sends an HTTP 415 error, sets the "Accept" - * header, and returns an empty {@code ModelAndView}. Alternatively, a fallback view could be chosen, or the - * HttpMediaTypeNotSupportedException could be rethrown as-is. - * + * were found for the PUT or POSTed content.

The default implementation sends an HTTP 415 error, + * sets the "Accept" header, and returns an empty {@code ModelAndView}. Alternatively, a fallback + * view could be chosen, or the HttpMediaTypeNotSupportedException could be rethrown as-is. * @param ex the HttpMediaTypeNotSupportedException to be handled * @param request current HTTP request * @param response current HTTP response - * @param handler the executed handler, or null if none chosen at the time of the exception (for example, - * if multipart resolution failed) + * @param handler the executed handler, or null if none chosen + * at the time of the exception (for example, if multipart resolution failed) * @return a ModelAndView to render, or null if handled directly * @throws Exception an Exception that should be thrown as result of the servlet request */ protected ModelAndView handleHttpMediaTypeNotSupported(HttpMediaTypeNotSupportedException ex, - HttpServletRequest request, - HttpServletResponse response, - Object handler) throws Exception { + HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { response.sendError(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE); List mediaTypes = ex.getSupportedMediaTypes(); @@ -202,131 +196,119 @@ public class DefaultHandlerExceptionResolver extends AbstractHandlerExceptionRes /** * Handle the case where no {@linkplain org.springframework.http.converter.HttpMessageConverter message converters} * were found that were acceptable for the client (expressed via the {@code Accept} header. - *

The default implementation sends an HTTP 406 error and returns an empty {@code ModelAndView}. Alternatively, - * a fallback view could be chosen, or the HttpMediaTypeNotAcceptableException could be rethrown as-is. - * + *

The default implementation sends an HTTP 406 error and returns an empty {@code ModelAndView}. + * Alternatively, a fallback view could be chosen, or the HttpMediaTypeNotAcceptableException + * could be rethrown as-is. * @param ex the HttpMediaTypeNotAcceptableException to be handled * @param request current HTTP request * @param response current HTTP response - * @param handler the executed handler, or null if none chosen at the time of the exception (for example, - * if multipart resolution failed) + * @param handler the executed handler, or null if none chosen + * at the time of the exception (for example, if multipart resolution failed) * @return a ModelAndView to render, or null if handled directly * @throws Exception an Exception that should be thrown as result of the servlet request */ protected ModelAndView handleHttpMediaTypeNotAcceptable(HttpMediaTypeNotAcceptableException ex, - HttpServletRequest request, - HttpServletResponse response, - Object handler) throws Exception { + HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { response.sendError(HttpServletResponse.SC_NOT_ACCEPTABLE); return new ModelAndView(); } /** - * Handle the case when a required parameter is missing.

The default implementation sends an HTTP 400 error, and - * returns an empty {@code ModelAndView}. Alternatively, a fallback view could be chosen, or the - * MissingServletRequestParameterException could be rethrown as-is. - * + * Handle the case when a required parameter is missing. + *

The default implementation sends an HTTP 400 error, and returns an empty {@code ModelAndView}. + * Alternatively, a fallback view could be chosen, or the MissingServletRequestParameterException + * could be rethrown as-is. * @param ex the MissingServletRequestParameterException to be handled * @param request current HTTP request * @param response current HTTP response - * @param handler the executed handler, or null if none chosen at the time of the exception (for example, - * if multipart resolution failed) + * @param handler the executed handler, or null if none chosen + * at the time of the exception (for example, if multipart resolution failed) * @return a ModelAndView to render, or null if handled directly * @throws Exception an Exception that should be thrown as result of the servlet request */ protected ModelAndView handleMissingServletRequestParameter(MissingServletRequestParameterException ex, - HttpServletRequest request, - HttpServletResponse response, - Object handler) throws Exception { + HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { response.sendError(HttpServletResponse.SC_BAD_REQUEST); return new ModelAndView(); } /** - * Handle the case when a {@link org.springframework.web.bind.WebDataBinder} conversion cannot occur.

The default - * implementation sends an HTTP 500 error, and returns an empty {@code ModelAndView}. Alternatively, a fallback view - * could be chosen, or the TypeMismatchException could be rethrown as-is. - * + * Handle the case when a {@link org.springframework.web.bind.WebDataBinder} conversion cannot occur. + *

The default implementation sends an HTTP 500 error, and returns an empty {@code ModelAndView}. + * Alternatively, a fallback view could be chosen, or the TypeMismatchException could be rethrown as-is. * @param ex the ConversionNotSupportedException to be handled * @param request current HTTP request * @param response current HTTP response - * @param handler the executed handler, or null if none chosen at the time of the exception (for example, - * if multipart resolution failed) + * @param handler the executed handler, or null if none chosen + * at the time of the exception (for example, if multipart resolution failed) * @return a ModelAndView to render, or null if handled directly * @throws Exception an Exception that should be thrown as result of the servlet request */ protected ModelAndView handleConversionNotSupported(ConversionNotSupportedException ex, - HttpServletRequest request, - HttpServletResponse response, - Object handler) throws Exception { + HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { + response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); return new ModelAndView(); } /** - * Handle the case when a {@link org.springframework.web.bind.WebDataBinder} conversion error occurs.

The default - * implementation sends an HTTP 400 error, and returns an empty {@code ModelAndView}. Alternatively, a fallback view - * could be chosen, or the TypeMismatchException could be rethrown as-is. - * + * Handle the case when a {@link org.springframework.web.bind.WebDataBinder} conversion error occurs. + *

The default implementation sends an HTTP 400 error, and returns an empty {@code ModelAndView}. + * Alternatively, a fallback view could be chosen, or the TypeMismatchException could be rethrown as-is. * @param ex the TypeMismatchException to be handled * @param request current HTTP request * @param response current HTTP response - * @param handler the executed handler, or null if none chosen at the time of the exception (for example, - * if multipart resolution failed) + * @param handler the executed handler, or null if none chosen + * at the time of the exception (for example, if multipart resolution failed) * @return a ModelAndView to render, or null if handled directly * @throws Exception an Exception that should be thrown as result of the servlet request */ protected ModelAndView handleTypeMismatch(TypeMismatchException ex, - HttpServletRequest request, - HttpServletResponse response, - Object handler) throws Exception { + HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { + response.sendError(HttpServletResponse.SC_BAD_REQUEST); return new ModelAndView(); } /** - * Handle the case where a {@linkplain org.springframework.http.converter.HttpMessageConverter message converter} can - * not read from a HTTP request.

The default implementation sends an HTTP 400 error, and returns an empty {@code - * ModelAndView}. Alternatively, a fallback view could be chosen, or the HttpMediaTypeNotSupportedException could be + * Handle the case where a {@linkplain org.springframework.http.converter.HttpMessageConverter message converter} + * cannot read from a HTTP request. + *

The default implementation sends an HTTP 400 error, and returns an empty {@code ModelAndView}. + * Alternatively, a fallback view could be chosen, or the HttpMediaTypeNotSupportedException could be * rethrown as-is. - * * @param ex the HttpMessageNotReadableException to be handled * @param request current HTTP request * @param response current HTTP response - * @param handler the executed handler, or null if none chosen at the time of the exception (for example, - * if multipart resolution failed) + * @param handler the executed handler, or null if none chosen + * at the time of the exception (for example, if multipart resolution failed) * @return a ModelAndView to render, or null if handled directly * @throws Exception an Exception that should be thrown as result of the servlet request */ protected ModelAndView handleHttpMessageNotReadable(HttpMessageNotReadableException ex, - HttpServletRequest request, - HttpServletResponse response, - Object handler) throws Exception { + HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { response.sendError(HttpServletResponse.SC_BAD_REQUEST); return new ModelAndView(); } /** - * Handle the case where a {@linkplain org.springframework.http.converter.HttpMessageConverter message converter} can - * not write to a HTTP request.

The default implementation sends an HTTP 500 error, and returns an empty {@code - * ModelAndView}. Alternatively, a fallback view could be chosen, or the HttpMediaTypeNotSupportedException could be + * Handle the case where a {@linkplain org.springframework.http.converter.HttpMessageConverter message converter} + * cannot write to a HTTP request. + *

The default implementation sends an HTTP 500 error, and returns an empty {@code ModelAndView}. + * Alternatively, a fallback view could be chosen, or the HttpMediaTypeNotSupportedException could be * rethrown as-is. - * * @param ex the HttpMessageNotWritableException to be handled * @param request current HTTP request * @param response current HTTP response - * @param handler the executed handler, or null if none chosen at the time of the exception (for example, - * if multipart resolution failed) + * @param handler the executed handler, or null if none chosen + * at the time of the exception (for example, if multipart resolution failed) * @return a ModelAndView to render, or null if handled directly * @throws Exception an Exception that should be thrown as result of the servlet request */ protected ModelAndView handleHttpMessageNotWritable(HttpMessageNotWritableException ex, - HttpServletRequest request, - HttpServletResponse response, - Object handler) throws Exception { + HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); return new ModelAndView(); diff --git a/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/support/WebContentGenerator.java b/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/support/WebContentGenerator.java index cc0bac35afd..793dff093ee 100644 --- a/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/support/WebContentGenerator.java +++ b/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/support/WebContentGenerator.java @@ -216,7 +216,7 @@ public abstract class WebContentGenerator extends WebApplicationObjectSupport { */ protected final void checkAndPrepare( HttpServletRequest request, HttpServletResponse response, boolean lastModified) - throws ServletException { + throws ServletException { checkAndPrepare(request, response, this.cacheSeconds, lastModified); } @@ -234,7 +234,7 @@ public abstract class WebContentGenerator extends WebApplicationObjectSupport { */ protected final void checkAndPrepare( HttpServletRequest request, HttpServletResponse response, int cacheSeconds, boolean lastModified) - throws ServletException { + throws ServletException { // Check whether we should support the request method. String method = request.getMethod();