SPR-5426 - Allow for custom processing or result objects returned from handler/controller methods

This commit is contained in:
Arjen Poutsma
2009-05-05 11:40:36 +00:00
parent a6124793fc
commit 0a0938fdd6
5 changed files with 270 additions and 26 deletions

View File

@@ -93,6 +93,7 @@ import org.springframework.web.portlet.handler.PortletContentGenerator;
import org.springframework.web.portlet.handler.PortletSessionRequiredException;
import org.springframework.web.portlet.util.PortletUtils;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.mvc.annotation.ModelAndViewResolver;
/**
* Implementation of the {@link org.springframework.web.portlet.HandlerAdapter}
@@ -131,6 +132,8 @@ public class AnnotationMethodHandlerAdapter extends PortletContentGenerator impl
private WebArgumentResolver[] customArgumentResolvers;
private ModelAndViewResolver[] customModelAndViewResolvers;
private final Map<Class<?>, PortletHandlerMethodResolver> methodResolverCache =
new ConcurrentHashMap<Class<?>, PortletHandlerMethodResolver>();
@@ -199,8 +202,8 @@ public class AnnotationMethodHandlerAdapter extends PortletContentGenerator impl
}
/**
* Set a custom ArgumentResolvers to use for special method parameter types.
* Such a custom ArgumentResolver will kick in first, having a chance to
* Set a custom WebArgumentResolvers to use for special method parameter types.
* Such a custom WebArgumentResolver will kick in first, having a chance to
* resolve an argument value before the standard argument handling kicks in.
*/
public void setCustomArgumentResolver(WebArgumentResolver argumentResolver) {
@@ -208,8 +211,8 @@ public class AnnotationMethodHandlerAdapter extends PortletContentGenerator impl
}
/**
* Set one or more custom ArgumentResolvers to use for special method
* parameter types. Any such custom ArgumentResolver will kick in first,
* Set one or more custom WebArgumentResolvers to use for special method
* parameter types. Any such custom WebArgumentResolver will kick in first,
* having a chance to resolve an argument value before the standard
* argument handling kicks in.
*/
@@ -217,6 +220,22 @@ public class AnnotationMethodHandlerAdapter extends PortletContentGenerator impl
this.customArgumentResolvers = argumentResolvers;
}
/**
* Set a custom ModelAndViewResolvers to use for special method return types. Such a custom ModelAndViewResolver will kick
* in first, having a chance to resolve an return value before the standard ModelAndView handling kicks in.
*/
public void setCustomModelAndViewResolver(ModelAndViewResolver customModelAndViewResolver) {
this.customModelAndViewResolvers = new ModelAndViewResolver[]{customModelAndViewResolver};
}
/**
* Set one or more custom ModelAndViewResolvers to use for special method return types. Any such custom ModelAndViewResolver
* will kick in first, having a chance to resolve an return value before the standard ModelAndView handling kicks in.
*/
public void setCustomModelAndViewResolvers(ModelAndViewResolver[] customModelAndViewResolvers) {
this.customModelAndViewResolvers = customModelAndViewResolvers;
}
public boolean supports(Object handler) {
return getMethodResolver(handler).hasHandlerMethods();
@@ -296,7 +315,8 @@ public class AnnotationMethodHandlerAdapter extends PortletContentGenerator impl
PortletHandlerMethodInvoker methodInvoker = new PortletHandlerMethodInvoker(methodResolver);
Object result = methodInvoker.invokeHandlerMethod(handlerMethod, handler, webRequest, implicitModel);
ModelAndView mav = methodInvoker.getModelAndView(handlerMethod, handler.getClass(), result, implicitModel);
ModelAndView mav = methodInvoker.getModelAndView(handlerMethod, handler.getClass(), result, implicitModel,
webRequest);
methodInvoker.updateModelAttributes(
handler, (mav != null ? mav.getModel() : null), implicitModel, webRequest);
@@ -660,8 +680,20 @@ public class AnnotationMethodHandlerAdapter extends PortletContentGenerator impl
}
@SuppressWarnings("unchecked")
public ModelAndView getModelAndView(
Method handlerMethod, Class handlerType, Object returnValue, ExtendedModelMap implicitModel) {
public ModelAndView getModelAndView(Method handlerMethod, Class handlerType, Object returnValue, ExtendedModelMap implicitModel,
PortletWebRequest webRequest) {
// Invoke custom resolvers if present...
if (customModelAndViewResolvers != null) {
for (ModelAndViewResolver mavResolver : customModelAndViewResolvers) {
org.springframework.web.servlet.ModelAndView smav = mavResolver
.resolveModelAndView(handlerMethod, handlerType, returnValue, implicitModel, webRequest);
if (smav != ModelAndViewResolver.UNRESOLVED) {
return (smav.isReference() ?
new ModelAndView(smav.getViewName(), smav.getModelMap()) :
new ModelAndView(smav.getView(), smav.getModelMap()));
}
}
}
if (returnValue instanceof ModelAndView) {
ModelAndView mav = (ModelAndView) returnValue;