initial BindingLifecycle @MVC integration
This commit is contained in:
@@ -19,6 +19,9 @@ package org.springframework.web.bind.annotation.support;
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.ParameterizedType;
|
||||
import java.lang.reflect.Type;
|
||||
import java.lang.reflect.TypeVariable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
@@ -29,7 +32,6 @@ import java.util.Set;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.core.BridgeMethodResolver;
|
||||
import org.springframework.core.Conventions;
|
||||
@@ -40,8 +42,11 @@ import org.springframework.core.annotation.AnnotationUtils;
|
||||
import org.springframework.http.HttpInputMessage;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.converter.HttpMessageConverter;
|
||||
import org.springframework.model.ui.PresentationModelFactory;
|
||||
import org.springframework.model.ui.config.BindingLifecycle;
|
||||
import org.springframework.ui.ExtendedModelMap;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.ui.MvcBindingLifecycle;
|
||||
import org.springframework.util.ClassUtils;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
@@ -64,8 +69,10 @@ import org.springframework.web.bind.support.WebArgumentResolver;
|
||||
import org.springframework.web.bind.support.WebBindingInitializer;
|
||||
import org.springframework.web.bind.support.WebRequestDataBinder;
|
||||
import org.springframework.web.context.request.NativeWebRequest;
|
||||
import org.springframework.web.context.request.NativeWebRequestParameterMap;
|
||||
import org.springframework.web.context.request.WebRequest;
|
||||
import org.springframework.web.multipart.MultipartRequest;
|
||||
import org.springframework.web.servlet.support.PresentationModelUtils;
|
||||
|
||||
/**
|
||||
* Support class for invoking an annotated handler method. Operates on the introspection results of a {@link
|
||||
@@ -237,6 +244,13 @@ public class HandlerMethodInvoker {
|
||||
throw new IllegalStateException("Errors/BindingResult argument declared " +
|
||||
"without preceding model attribute. Check your handler method signature!");
|
||||
}
|
||||
// TODO - Code Review - NEW BINDING LIFECYCLE RESOLVABLE ARG
|
||||
else if (BindingLifecycle.class.isAssignableFrom(paramType)) {
|
||||
Class<?> modelType = resolveBindingLifecycleModelType(methodParam);
|
||||
PresentationModelFactory factory = PresentationModelUtils.getPresentationModelFactory(webRequest);
|
||||
Map<String, Object> fieldValues = new NativeWebRequestParameterMap(webRequest);
|
||||
args[i] = new MvcBindingLifecycle(modelType, factory, implicitModel, fieldValues);
|
||||
}
|
||||
else if (BeanUtils.isSimpleProperty(paramType)) {
|
||||
paramName = "";
|
||||
}
|
||||
@@ -696,7 +710,7 @@ public class HandlerMethodInvoker {
|
||||
}
|
||||
return WebArgumentResolver.UNRESOLVED;
|
||||
}
|
||||
|
||||
|
||||
protected final void addReturnValueAsModelAttribute(
|
||||
Method handlerMethod, Class handlerType, Object returnValue, ExtendedModelMap implicitModel) {
|
||||
|
||||
@@ -709,4 +723,23 @@ public class HandlerMethodInvoker {
|
||||
implicitModel.addAttribute(attrName, returnValue);
|
||||
}
|
||||
|
||||
// TODO - Code Review - BINDING LIFECYCLE RELATED INTERNAL HELPERS
|
||||
|
||||
// TODO - this generic arg identification looping code is duplicated in several places now...
|
||||
private Class<?> resolveBindingLifecycleModelType(MethodParameter methodParam) {
|
||||
Type type = GenericTypeResolver.getTargetType(methodParam);
|
||||
if (type instanceof ParameterizedType) {
|
||||
ParameterizedType paramType = (ParameterizedType) type;
|
||||
Type rawType = paramType.getRawType();
|
||||
Type arg = paramType.getActualTypeArguments()[0];
|
||||
if (arg instanceof TypeVariable) {
|
||||
arg = GenericTypeResolver.resolveTypeVariable((TypeVariable) arg, BindingLifecycle.class);
|
||||
}
|
||||
if (arg instanceof Class) {
|
||||
return (Class) arg;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
* Copyright 2004-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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.web.servlet.support;
|
||||
|
||||
import javax.servlet.ServletRequest;
|
||||
|
||||
import org.springframework.model.ui.PresentationModelFactory;
|
||||
import org.springframework.model.ui.support.DefaultPresentationModelFactory;
|
||||
import org.springframework.web.context.request.WebRequest;
|
||||
|
||||
/**
|
||||
* Utilities for working with the <code>model.ui</code> PresentationModel system.
|
||||
* @author Keith Donald
|
||||
*/
|
||||
public final class PresentationModelUtils {
|
||||
|
||||
private static final String PRESENTATION_MODEL_FACTORY_ATTRIBUTE = "presentationModelFactory";
|
||||
|
||||
private PresentationModelUtils() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the PresentationModelFactory for the current web request.
|
||||
* Will create a new one and cache it as a request attribute if one does not exist.
|
||||
* @param request the web request
|
||||
* @return the presentation model factory
|
||||
*/
|
||||
public static PresentationModelFactory getPresentationModelFactory(WebRequest request) {
|
||||
PresentationModelFactory factory = (PresentationModelFactory) request.getAttribute(PRESENTATION_MODEL_FACTORY_ATTRIBUTE, WebRequest.SCOPE_REQUEST);
|
||||
if (factory == null) {
|
||||
factory = new DefaultPresentationModelFactory();
|
||||
request.setAttribute(PRESENTATION_MODEL_FACTORY_ATTRIBUTE, factory, WebRequest.SCOPE_REQUEST);
|
||||
}
|
||||
return factory;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the PresentationModelFactory for the current servlet request.
|
||||
* Will create a new one and cache it as a request attribute if one does not exist.
|
||||
* @param request the servlet
|
||||
* @return the presentation model factory
|
||||
*/
|
||||
public static PresentationModelFactory getPresentationModelFactory(ServletRequest request) {
|
||||
PresentationModelFactory factory = (PresentationModelFactory) request.getAttribute(PRESENTATION_MODEL_FACTORY_ATTRIBUTE);
|
||||
if (factory == null) {
|
||||
factory = new DefaultPresentationModelFactory();
|
||||
request.setAttribute(PRESENTATION_MODEL_FACTORY_ATTRIBUTE, factory);
|
||||
}
|
||||
return factory;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -16,6 +16,14 @@
|
||||
|
||||
package org.springframework.web.servlet.mvc.annotation;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertSame;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Serializable;
|
||||
import java.io.Writer;
|
||||
@@ -35,6 +43,7 @@ import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.servlet.ServletConfig;
|
||||
import javax.servlet.ServletContext;
|
||||
import javax.servlet.ServletException;
|
||||
@@ -43,9 +52,7 @@ import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import javax.servlet.http.HttpSession;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
|
||||
import org.springframework.aop.interceptor.SimpleTraceInterceptor;
|
||||
import org.springframework.aop.support.DefaultPointcutAdvisor;
|
||||
@@ -69,6 +76,7 @@ import org.springframework.mock.web.MockHttpServletRequest;
|
||||
import org.springframework.mock.web.MockHttpServletResponse;
|
||||
import org.springframework.mock.web.MockServletConfig;
|
||||
import org.springframework.mock.web.MockServletContext;
|
||||
import org.springframework.model.ui.config.BindingLifecycle;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.ExtendedModelMap;
|
||||
import org.springframework.ui.Model;
|
||||
@@ -1725,7 +1733,54 @@ public class ServletAnnotationControllerTests {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testBindingLifecycle() throws Exception {
|
||||
initServlet(BindingLifecycleController.class);
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/test");
|
||||
request.addParameter("bar", "test");
|
||||
request.addParameter("baz", "12");
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
servlet.service(request, response);
|
||||
assertEquals("bar=test, baz=12", response.getContentAsString());
|
||||
}
|
||||
|
||||
@Controller
|
||||
public static class BindingLifecycleController {
|
||||
|
||||
@RequestMapping("/test")
|
||||
public void bind(BindingLifecycle<Foo> fooLifecycle, Writer writer) throws IOException {
|
||||
fooLifecycle.execute();
|
||||
Foo foo = fooLifecycle.getModel();
|
||||
assertEquals("test", foo.getBar());
|
||||
assertEquals(new Integer(12), foo.getBaz());
|
||||
writer.write("bar=" + foo.getBar() + ", baz=" + foo.getBaz());
|
||||
}
|
||||
|
||||
public static final class Foo {
|
||||
|
||||
private String bar;
|
||||
|
||||
private Integer baz;
|
||||
|
||||
public String getBar() {
|
||||
return bar;
|
||||
}
|
||||
|
||||
public void setBar(String bar) {
|
||||
this.bar = bar;
|
||||
}
|
||||
|
||||
public Integer getBaz() {
|
||||
return baz;
|
||||
}
|
||||
|
||||
public void setBaz(Integer baz) {
|
||||
this.baz = baz;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user