|
|
|
@ -1,5 +1,5 @@ |
|
|
|
/* |
|
|
|
/* |
|
|
|
* Copyright 2002-2014 the original author or authors. |
|
|
|
* Copyright 2002-2016 the original author or authors. |
|
|
|
* |
|
|
|
* |
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
@ -45,14 +45,14 @@ import org.springframework.web.method.support.InvocableHandlerMethod; |
|
|
|
import org.springframework.web.method.support.ModelAndViewContainer; |
|
|
|
import org.springframework.web.method.support.ModelAndViewContainer; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Provides methods to initialize the {@link Model} before controller method |
|
|
|
* Assist with initialization of the {@link Model} before controller method |
|
|
|
* invocation and to update it afterwards. |
|
|
|
* invocation and with updates to it after the invocation. |
|
|
|
* |
|
|
|
* |
|
|
|
* <p>On initialization, the model is populated with attributes from the session |
|
|
|
* <p>On initialization the model is populated with attributes temporarily stored |
|
|
|
* and by invoking methods annotated with {@code @ModelAttribute}. |
|
|
|
* in the session and through the invocation of {@code @ModelAttribute} methods. |
|
|
|
* |
|
|
|
* |
|
|
|
* <p>On update, model attributes are synchronized with the session and also |
|
|
|
* <p>On update model attributes are synchronized with the session and also |
|
|
|
* {@link BindingResult} attributes are added where missing. |
|
|
|
* {@link BindingResult} attributes are added if missing. |
|
|
|
* |
|
|
|
* |
|
|
|
* @author Rossen Stoyanchev |
|
|
|
* @author Rossen Stoyanchev |
|
|
|
* @since 3.1 |
|
|
|
* @since 3.1 |
|
|
|
@ -70,51 +70,51 @@ public final class ModelFactory { |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Create a new instance with the given {@code @ModelAttribute} methods. |
|
|
|
* Create a new instance with the given {@code @ModelAttribute} methods. |
|
|
|
* @param invocableMethods the {@code @ModelAttribute} methods to invoke |
|
|
|
* @param handlerMethods the {@code @ModelAttribute} methods to invoke |
|
|
|
* @param dataBinderFactory for preparation of {@link BindingResult} attributes |
|
|
|
* @param binderFactory for preparation of {@link BindingResult} attributes |
|
|
|
* @param sessionAttributesHandler for access to session attributes |
|
|
|
* @param attributeHandler for access to session attributes |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public ModelFactory(List<InvocableHandlerMethod> invocableMethods, WebDataBinderFactory dataBinderFactory, |
|
|
|
public ModelFactory(List<InvocableHandlerMethod> handlerMethods, |
|
|
|
SessionAttributesHandler sessionAttributesHandler) { |
|
|
|
WebDataBinderFactory binderFactory, SessionAttributesHandler attributeHandler) { |
|
|
|
|
|
|
|
|
|
|
|
if (invocableMethods != null) { |
|
|
|
if (handlerMethods != null) { |
|
|
|
for (InvocableHandlerMethod method : invocableMethods) { |
|
|
|
for (InvocableHandlerMethod handlerMethod : handlerMethods) { |
|
|
|
this.modelMethods.add(new ModelMethod(method)); |
|
|
|
this.modelMethods.add(new ModelMethod(handlerMethod)); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
this.dataBinderFactory = dataBinderFactory; |
|
|
|
this.dataBinderFactory = binderFactory; |
|
|
|
this.sessionAttributesHandler = sessionAttributesHandler; |
|
|
|
this.sessionAttributesHandler = attributeHandler; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Populate the model in the following order: |
|
|
|
* Populate the model in the following order: |
|
|
|
* <ol> |
|
|
|
* <ol> |
|
|
|
* <li>Retrieve "known" session attributes listed as {@code @SessionAttributes}. |
|
|
|
* <li>Retrieve "known" session attributes listed as {@code @SessionAttributes}. |
|
|
|
* <li>Invoke {@code @ModelAttribute} methods |
|
|
|
* <li>Invoke {@code @ModelAttribute} methods |
|
|
|
* <li>Find {@code @ModelAttribute} method arguments also listed as |
|
|
|
* <li>Find {@code @ModelAttribute} method arguments also listed as |
|
|
|
* {@code @SessionAttributes} and ensure they're present in the model raising |
|
|
|
* {@code @SessionAttributes} and ensure they're present in the model raising |
|
|
|
* an exception if necessary. |
|
|
|
* an exception if necessary. |
|
|
|
* </ol> |
|
|
|
* </ol> |
|
|
|
* @param request the current request |
|
|
|
* @param request the current request |
|
|
|
* @param mavContainer a container with the model to be initialized |
|
|
|
* @param container a container with the model to be initialized |
|
|
|
* @param handlerMethod the method for which the model is initialized |
|
|
|
* @param handlerMethod the method for which the model is initialized |
|
|
|
* @throws Exception may arise from {@code @ModelAttribute} methods |
|
|
|
* @throws Exception may arise from {@code @ModelAttribute} methods |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public void initModel(NativeWebRequest request, ModelAndViewContainer mavContainer, HandlerMethod handlerMethod) |
|
|
|
public void initModel(NativeWebRequest request, ModelAndViewContainer container, |
|
|
|
throws Exception { |
|
|
|
HandlerMethod handlerMethod) throws Exception { |
|
|
|
|
|
|
|
|
|
|
|
Map<String, ?> sessionAttributes = this.sessionAttributesHandler.retrieveAttributes(request); |
|
|
|
Map<String, ?> sessionAttributes = this.sessionAttributesHandler.retrieveAttributes(request); |
|
|
|
mavContainer.mergeAttributes(sessionAttributes); |
|
|
|
container.mergeAttributes(sessionAttributes); |
|
|
|
|
|
|
|
invokeModelAttributeMethods(request, container); |
|
|
|
invokeModelAttributeMethods(request, mavContainer); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (String name : findSessionAttributeArguments(handlerMethod)) { |
|
|
|
for (String name : findSessionAttributeArguments(handlerMethod)) { |
|
|
|
if (!mavContainer.containsAttribute(name)) { |
|
|
|
if (!container.containsAttribute(name)) { |
|
|
|
Object value = this.sessionAttributesHandler.retrieveAttribute(request, name); |
|
|
|
Object value = this.sessionAttributesHandler.retrieveAttribute(request, name); |
|
|
|
if (value == null) { |
|
|
|
if (value == null) { |
|
|
|
throw new HttpSessionRequiredException("Expected session attribute '" + name + "'"); |
|
|
|
throw new HttpSessionRequiredException("Expected session attribute '" + name + "'"); |
|
|
|
} |
|
|
|
} |
|
|
|
mavContainer.addAttribute(name, value); |
|
|
|
container.addAttribute(name, value); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
@ -123,30 +123,29 @@ public final class ModelFactory { |
|
|
|
* Invoke model attribute methods to populate the model. |
|
|
|
* Invoke model attribute methods to populate the model. |
|
|
|
* Attributes are added only if not already present in the model. |
|
|
|
* Attributes are added only if not already present in the model. |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private void invokeModelAttributeMethods(NativeWebRequest request, ModelAndViewContainer mavContainer) |
|
|
|
private void invokeModelAttributeMethods(NativeWebRequest request, ModelAndViewContainer container) |
|
|
|
throws Exception { |
|
|
|
throws Exception { |
|
|
|
|
|
|
|
|
|
|
|
while (!this.modelMethods.isEmpty()) { |
|
|
|
while (!this.modelMethods.isEmpty()) { |
|
|
|
InvocableHandlerMethod attrMethod = getNextModelMethod(mavContainer).getHandlerMethod(); |
|
|
|
InvocableHandlerMethod modelMethod = getNextModelMethod(container).getHandlerMethod(); |
|
|
|
String modelName = attrMethod.getMethodAnnotation(ModelAttribute.class).value(); |
|
|
|
String modelName = modelMethod.getMethodAnnotation(ModelAttribute.class).value(); |
|
|
|
if (mavContainer.containsAttribute(modelName)) { |
|
|
|
if (container.containsAttribute(modelName)) { |
|
|
|
continue; |
|
|
|
continue; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
Object returnValue = attrMethod.invokeForRequest(request, mavContainer); |
|
|
|
Object returnValue = modelMethod.invokeForRequest(request, container); |
|
|
|
|
|
|
|
if (!modelMethod.isVoid()){ |
|
|
|
if (!attrMethod.isVoid()){ |
|
|
|
String returnValueName = getNameForReturnValue(returnValue, modelMethod.getReturnType()); |
|
|
|
String returnValueName = getNameForReturnValue(returnValue, attrMethod.getReturnType()); |
|
|
|
if (!container.containsAttribute(returnValueName)) { |
|
|
|
if (!mavContainer.containsAttribute(returnValueName)) { |
|
|
|
container.addAttribute(returnValueName, returnValue); |
|
|
|
mavContainer.addAttribute(returnValueName, returnValue); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private ModelMethod getNextModelMethod(ModelAndViewContainer mavContainer) { |
|
|
|
private ModelMethod getNextModelMethod(ModelAndViewContainer container) { |
|
|
|
for (ModelMethod modelMethod : this.modelMethods) { |
|
|
|
for (ModelMethod modelMethod : this.modelMethods) { |
|
|
|
if (modelMethod.checkDependencies(mavContainer)) { |
|
|
|
if (modelMethod.checkDependencies(container)) { |
|
|
|
if (logger.isTraceEnabled()) { |
|
|
|
if (logger.isTraceEnabled()) { |
|
|
|
logger.trace("Selected @ModelAttribute method " + modelMethod); |
|
|
|
logger.trace("Selected @ModelAttribute method " + modelMethod); |
|
|
|
} |
|
|
|
} |
|
|
|
@ -157,7 +156,7 @@ public final class ModelFactory { |
|
|
|
ModelMethod modelMethod = this.modelMethods.get(0); |
|
|
|
ModelMethod modelMethod = this.modelMethods.get(0); |
|
|
|
if (logger.isTraceEnabled()) { |
|
|
|
if (logger.isTraceEnabled()) { |
|
|
|
logger.trace("Selected @ModelAttribute method (not present: " + |
|
|
|
logger.trace("Selected @ModelAttribute method (not present: " + |
|
|
|
modelMethod.getUnresolvedDependencies(mavContainer)+ ") " + modelMethod); |
|
|
|
modelMethod.getUnresolvedDependencies(container)+ ") " + modelMethod); |
|
|
|
} |
|
|
|
} |
|
|
|
this.modelMethods.remove(modelMethod); |
|
|
|
this.modelMethods.remove(modelMethod); |
|
|
|
return modelMethod; |
|
|
|
return modelMethod; |
|
|
|
@ -171,7 +170,8 @@ public final class ModelFactory { |
|
|
|
for (MethodParameter parameter : handlerMethod.getMethodParameters()) { |
|
|
|
for (MethodParameter parameter : handlerMethod.getMethodParameters()) { |
|
|
|
if (parameter.hasParameterAnnotation(ModelAttribute.class)) { |
|
|
|
if (parameter.hasParameterAnnotation(ModelAttribute.class)) { |
|
|
|
String name = getNameForParameter(parameter); |
|
|
|
String name = getNameForParameter(parameter); |
|
|
|
if (this.sessionAttributesHandler.isHandlerSessionAttribute(name, parameter.getParameterType())) { |
|
|
|
Class<?> paramType = parameter.getParameterType(); |
|
|
|
|
|
|
|
if (this.sessionAttributesHandler.isHandlerSessionAttribute(name, paramType)) { |
|
|
|
result.add(name); |
|
|
|
result.add(name); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
@ -182,36 +182,37 @@ public final class ModelFactory { |
|
|
|
/** |
|
|
|
/** |
|
|
|
* Derives the model attribute name for a method parameter based on: |
|
|
|
* Derives the model attribute name for a method parameter based on: |
|
|
|
* <ol> |
|
|
|
* <ol> |
|
|
|
* <li>The parameter {@code @ModelAttribute} annotation value |
|
|
|
* <li>The parameter {@code @ModelAttribute} annotation value |
|
|
|
* <li>The parameter type |
|
|
|
* <li>The parameter type |
|
|
|
* </ol> |
|
|
|
* </ol> |
|
|
|
* @return the derived name; never {@code null} or an empty string |
|
|
|
* @return the derived name; never {@code null} or an empty string |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public static String getNameForParameter(MethodParameter parameter) { |
|
|
|
public static String getNameForParameter(MethodParameter parameter) { |
|
|
|
ModelAttribute annot = parameter.getParameterAnnotation(ModelAttribute.class); |
|
|
|
ModelAttribute ann = parameter.getParameterAnnotation(ModelAttribute.class); |
|
|
|
String attrName = (annot != null) ? annot.value() : null; |
|
|
|
String name = (ann != null ? ann.value() : null); |
|
|
|
return StringUtils.hasText(attrName) ? attrName : Conventions.getVariableNameForParameter(parameter); |
|
|
|
return StringUtils.hasText(name) ? name : Conventions.getVariableNameForParameter(parameter); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Derive the model attribute name for the given return value using one of: |
|
|
|
* Derive the model attribute name for the given return value using one of: |
|
|
|
* <ol> |
|
|
|
* <ol> |
|
|
|
* <li>The method {@code ModelAttribute} annotation value |
|
|
|
* <li>The method {@code ModelAttribute} annotation value |
|
|
|
* <li>The declared return type if it is more specific than {@code Object} |
|
|
|
* <li>The declared return type if it is more specific than {@code Object} |
|
|
|
* <li>The actual return value type |
|
|
|
* <li>The actual return value type |
|
|
|
* </ol> |
|
|
|
* </ol> |
|
|
|
* @param returnValue the value returned from a method invocation |
|
|
|
* @param returnValue the value returned from a method invocation |
|
|
|
* @param returnType the return type of the method |
|
|
|
* @param returnType the return type of the method |
|
|
|
* @return the model name, never {@code null} nor empty |
|
|
|
* @return the model name, never {@code null} nor empty |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public static String getNameForReturnValue(Object returnValue, MethodParameter returnType) { |
|
|
|
public static String getNameForReturnValue(Object returnValue, MethodParameter returnType) { |
|
|
|
ModelAttribute annotation = returnType.getMethodAnnotation(ModelAttribute.class); |
|
|
|
ModelAttribute ann = returnType.getMethodAnnotation(ModelAttribute.class); |
|
|
|
if (annotation != null && StringUtils.hasText(annotation.value())) { |
|
|
|
if (ann != null && StringUtils.hasText(ann.value())) { |
|
|
|
return annotation.value(); |
|
|
|
return ann.value(); |
|
|
|
} |
|
|
|
} |
|
|
|
else { |
|
|
|
else { |
|
|
|
Method method = returnType.getMethod(); |
|
|
|
Method method = returnType.getMethod(); |
|
|
|
Class<?> resolvedType = GenericTypeResolver.resolveReturnType(method, returnType.getContainingClass()); |
|
|
|
Class<?> containingClass = returnType.getContainingClass(); |
|
|
|
|
|
|
|
Class<?> resolvedType = GenericTypeResolver.resolveReturnType(method, containingClass); |
|
|
|
return Conventions.getVariableNameForReturnType(method, resolvedType, returnValue); |
|
|
|
return Conventions.getVariableNameForReturnType(method, resolvedType, returnValue); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
@ -220,18 +221,18 @@ public final class ModelFactory { |
|
|
|
* Promote model attributes listed as {@code @SessionAttributes} to the session. |
|
|
|
* Promote model attributes listed as {@code @SessionAttributes} to the session. |
|
|
|
* Add {@link BindingResult} attributes where necessary. |
|
|
|
* Add {@link BindingResult} attributes where necessary. |
|
|
|
* @param request the current request |
|
|
|
* @param request the current request |
|
|
|
* @param mavContainer contains the model to update |
|
|
|
* @param container contains the model to update |
|
|
|
* @throws Exception if creating BindingResult attributes fails |
|
|
|
* @throws Exception if creating BindingResult attributes fails |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public void updateModel(NativeWebRequest request, ModelAndViewContainer mavContainer) throws Exception { |
|
|
|
public void updateModel(NativeWebRequest request, ModelAndViewContainer container) throws Exception { |
|
|
|
ModelMap defaultModel = mavContainer.getDefaultModel(); |
|
|
|
ModelMap defaultModel = container.getDefaultModel(); |
|
|
|
if (mavContainer.getSessionStatus().isComplete()){ |
|
|
|
if (container.getSessionStatus().isComplete()){ |
|
|
|
this.sessionAttributesHandler.cleanupAttributes(request); |
|
|
|
this.sessionAttributesHandler.cleanupAttributes(request); |
|
|
|
} |
|
|
|
} |
|
|
|
else { |
|
|
|
else { |
|
|
|
this.sessionAttributesHandler.storeAttributes(request, defaultModel); |
|
|
|
this.sessionAttributesHandler.storeAttributes(request, defaultModel); |
|
|
|
} |
|
|
|
} |
|
|
|
if (!mavContainer.isRequestHandled() && mavContainer.getModel() == defaultModel) { |
|
|
|
if (!container.isRequestHandled() && container.getModel() == defaultModel) { |
|
|
|
updateBindingResult(request, defaultModel); |
|
|
|
updateBindingResult(request, defaultModel); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
@ -248,7 +249,7 @@ public final class ModelFactory { |
|
|
|
String bindingResultKey = BindingResult.MODEL_KEY_PREFIX + name; |
|
|
|
String bindingResultKey = BindingResult.MODEL_KEY_PREFIX + name; |
|
|
|
|
|
|
|
|
|
|
|
if (!model.containsAttribute(bindingResultKey)) { |
|
|
|
if (!model.containsAttribute(bindingResultKey)) { |
|
|
|
WebDataBinder dataBinder = dataBinderFactory.createBinder(request, value, name); |
|
|
|
WebDataBinder dataBinder = this.dataBinderFactory.createBinder(request, value, name); |
|
|
|
model.put(bindingResultKey, dataBinder.getBindingResult()); |
|
|
|
model.put(bindingResultKey, dataBinder.getBindingResult()); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
@ -279,7 +280,6 @@ public final class ModelFactory { |
|
|
|
|
|
|
|
|
|
|
|
private final Set<String> dependencies = new HashSet<String>(); |
|
|
|
private final Set<String> dependencies = new HashSet<String>(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private ModelMethod(InvocableHandlerMethod handlerMethod) { |
|
|
|
private ModelMethod(InvocableHandlerMethod handlerMethod) { |
|
|
|
this.handlerMethod = handlerMethod; |
|
|
|
this.handlerMethod = handlerMethod; |
|
|
|
for (MethodParameter parameter : handlerMethod.getMethodParameters()) { |
|
|
|
for (MethodParameter parameter : handlerMethod.getMethodParameters()) { |
|
|
|
|