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;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2008 the original author or authors.
* Copyright 2002-2009 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.
@@ -17,6 +17,7 @@
package org.springframework.web.portlet.mvc.annotation;
import java.io.IOException;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
@@ -32,6 +33,8 @@ import javax.portlet.PortletSession;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import javax.portlet.UnavailableException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import junit.framework.TestCase;
@@ -72,6 +75,8 @@ import org.springframework.web.portlet.DispatcherPortlet;
import org.springframework.web.portlet.ModelAndView;
import org.springframework.web.portlet.context.StaticPortletApplicationContext;
import org.springframework.web.portlet.mvc.AbstractController;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.mvc.annotation.ModelAndViewResolver;
/**
* @author Juergen Hoeller
@@ -434,6 +439,28 @@ public class PortletAnnotationControllerTests extends TestCase {
assertEquals("mySurpriseView", response.getContentAsString());
}
public void testMavResolver() throws Exception {
@SuppressWarnings("serial") DispatcherPortlet portlet = new DispatcherPortlet() {
@Override
protected ApplicationContext createPortletApplicationContext(ApplicationContext parent) throws BeansException {
GenericWebApplicationContext wac = new GenericWebApplicationContext();
wac.registerBeanDefinition("controller",
new RootBeanDefinition(ModelAndViewResolverController.class));
RootBeanDefinition adapterDef = new RootBeanDefinition(AnnotationMethodHandlerAdapter.class);
adapterDef.getPropertyValues()
.addPropertyValue("customModelAndViewResolver", new MyModelAndViewResolver());
wac.registerBeanDefinition("handlerAdapter", adapterDef);
wac.refresh();
return wac;
}
};
portlet.init(new MockPortletConfig());
MockRenderRequest request = new MockRenderRequest(PortletMode.VIEW);
MockRenderResponse response = new MockRenderResponse();
portlet.render(request, response);
}
@RequestMapping("VIEW")
private static class MyController extends AbstractController {
@@ -769,4 +796,37 @@ public class PortletAnnotationControllerTests extends TestCase {
}
}
@Controller
public static class ModelAndViewResolverController {
@RequestMapping("VIEW")
public MySpecialArg handle() {
return new MySpecialArg("foo");
}
}
public static class MyModelAndViewResolver implements ModelAndViewResolver {
public org.springframework.web.servlet.ModelAndView resolveModelAndView(Method handlerMethod,
Class handlerType,
Object returnValue,
ExtendedModelMap implicitModel,
NativeWebRequest webRequest) {
if (returnValue instanceof MySpecialArg) {
return new org.springframework.web.servlet.ModelAndView(new View() {
public String getContentType() {
return "text/html";
}
public void render(Map<String, ?> model, HttpServletRequest request, HttpServletResponse response)
throws Exception {
response.getWriter().write("myValue");
}
});
}
return UNRESOLVED;
}
}
}