Allow RedirectAttributes on ExceptionHandlers
Prior to this commit, `@ExceptionHandler` methods could not be injected
with `RedirectAttributes` arguments. This would make it impossible to
handle an error by redirecting to another view and add flashmap
attributes, to be included in the model when the next view is called.
Here is an example:
```
@ExceptionHandler(MyException.class)
public String handleException(MyException ex, RedirectAttributes
redirectAttributes) {
redirectAttributes.addFlashAttribute("errorMessage",
"This is an error message");
return "redirect:/";
}
```
This commit adds a new `RedirectAttributesMethodArgumentResolver`
instance in the list of pre-configured `HandlerMethodArgumentResolver`
in `ExceptionHandlerExceptionResolver`.
Issue: SPR-14651
This commit is contained in:
@@ -55,6 +55,8 @@ import org.springframework.web.method.support.ModelAndViewContainer;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
import org.springframework.web.servlet.View;
|
||||
import org.springframework.web.servlet.handler.AbstractHandlerMethodExceptionResolver;
|
||||
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
|
||||
import org.springframework.web.servlet.support.RequestContextUtils;
|
||||
|
||||
/**
|
||||
* An {@link AbstractHandlerMethodExceptionResolver} that resolves exceptions
|
||||
@@ -303,6 +305,7 @@ public class ExceptionHandlerExceptionResolver extends AbstractHandlerMethodExce
|
||||
// Type-based argument resolution
|
||||
resolvers.add(new ServletRequestMethodArgumentResolver());
|
||||
resolvers.add(new ServletResponseMethodArgumentResolver());
|
||||
resolvers.add(new RedirectAttributesMethodArgumentResolver());
|
||||
resolvers.add(new ModelMethodProcessor());
|
||||
|
||||
// Custom arguments
|
||||
@@ -401,6 +404,11 @@ public class ExceptionHandlerExceptionResolver extends AbstractHandlerMethodExce
|
||||
if (!mavContainer.isViewReference()) {
|
||||
mav.setView((View) mavContainer.getView());
|
||||
}
|
||||
if (model instanceof RedirectAttributes) {
|
||||
Map<String, ?> flashAttributes = ((RedirectAttributes) model).getFlashAttributes();
|
||||
request = webRequest.getNativeRequest(HttpServletRequest.class);
|
||||
RequestContextUtils.getOutputFlashMap(request).putAll(flashAttributes);
|
||||
}
|
||||
return mav;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -53,8 +53,14 @@ public class RedirectAttributesMethodArgumentResolver implements HandlerMethodAr
|
||||
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,
|
||||
NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
|
||||
|
||||
DataBinder dataBinder = binderFactory.createBinder(webRequest, null, null);
|
||||
ModelMap redirectAttributes = new RedirectAttributesModelMap(dataBinder);
|
||||
ModelMap redirectAttributes;
|
||||
if(binderFactory != null) {
|
||||
DataBinder dataBinder = binderFactory.createBinder(webRequest, null, null);
|
||||
redirectAttributes = new RedirectAttributesModelMap(dataBinder);
|
||||
}
|
||||
else {
|
||||
redirectAttributes = new RedirectAttributesModelMap();
|
||||
}
|
||||
mavContainer.setRedirectModel(redirectAttributes);
|
||||
return redirectAttributes;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user