11/*
2- * Copyright 2002-2012 the original author or authors.
2+ * Copyright 2002-2014 the original author or authors.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
2121
2222import org .apache .commons .logging .Log ;
2323import org .apache .commons .logging .LogFactory ;
24+
2425import org .springframework .beans .BeanUtils ;
2526import org .springframework .core .MethodParameter ;
2627import org .springframework .core .annotation .AnnotationUtils ;
@@ -58,6 +59,7 @@ public class ModelAttributeMethodProcessor implements HandlerMethodArgumentResol
5859
5960 private final boolean annotationNotRequired ;
6061
62+
6163 /**
6264 * @param annotationNotRequired if "true", non-simple method arguments and
6365 * return values are considered model attributes with or without a
@@ -67,6 +69,7 @@ public ModelAttributeMethodProcessor(boolean annotationNotRequired) {
6769 this .annotationNotRequired = annotationNotRequired ;
6870 }
6971
72+
7073 /**
7174 * @return true if the parameter is annotated with {@link ModelAttribute}
7275 * or in default resolution mode also if it is not a simple type.
@@ -94,18 +97,16 @@ else if (this.annotationNotRequired) {
9497 * @throws Exception if WebDataBinder initialization fails.
9598 */
9699 @ Override
97- public final Object resolveArgument (
98- MethodParameter parameter , ModelAndViewContainer mavContainer ,
99- NativeWebRequest request , WebDataBinderFactory binderFactory )
100- throws Exception {
100+ public final Object resolveArgument (MethodParameter parameter , ModelAndViewContainer mavContainer ,
101+ NativeWebRequest webRequest , WebDataBinderFactory binderFactory ) throws Exception {
101102
102103 String name = ModelFactory .getNameForParameter (parameter );
103- Object attribute = (mavContainer .containsAttribute (name )) ?
104- mavContainer .getModel ().get (name ) : createAttribute (name , parameter , binderFactory , request );
104+ Object attribute = (mavContainer .containsAttribute (name ) ?
105+ mavContainer .getModel ().get (name ) : createAttribute (name , parameter , binderFactory , webRequest ) );
105106
106- WebDataBinder binder = binderFactory .createBinder (request , attribute , name );
107+ WebDataBinder binder = binderFactory .createBinder (webRequest , attribute , name );
107108 if (binder .getTarget () != null ) {
108- bindRequestParameters (binder , request );
109+ bindRequestParameters (binder , webRequest );
109110 validateIfApplicable (binder , parameter );
110111 if (binder .getBindingResult ().hasErrors ()) {
111112 if (isBindExceptionRequired (binder , parameter )) {
@@ -120,17 +121,17 @@ public final Object resolveArgument(
120121 mavContainer .removeAttributes (bindingResultModel );
121122 mavContainer .addAllAttributes (bindingResultModel );
122123
123- return binder .getTarget ();
124+ return binder .convertIfNecessary ( binder . getTarget (), parameter . getParameterType (), parameter );
124125 }
125126
126127 /**
127128 * Extension point to create the model attribute if not found in the model.
128129 * The default implementation uses the default constructor.
129- * @param attributeName the name of the attribute, never {@code null}
130+ * @param attributeName the name of the attribute ( never {@code null})
130131 * @param parameter the method parameter
131132 * @param binderFactory for creating WebDataBinder instance
132133 * @param request the current request
133- * @return the created model attribute, never {@code null}
134+ * @return the created model attribute ( never {@code null})
134135 */
135136 protected Object createAttribute (String attributeName , MethodParameter parameter ,
136137 WebDataBinderFactory binderFactory , NativeWebRequest request ) throws Exception {
@@ -155,9 +156,9 @@ protected void bindRequestParameters(WebDataBinder binder, NativeWebRequest requ
155156 */
156157 protected void validateIfApplicable (WebDataBinder binder , MethodParameter parameter ) {
157158 Annotation [] annotations = parameter .getParameterAnnotations ();
158- for (Annotation annot : annotations ) {
159- if (annot .annotationType ().getSimpleName ().startsWith ("Valid" )) {
160- Object hints = AnnotationUtils .getValue (annot );
159+ for (Annotation ann : annotations ) {
160+ if (ann .annotationType ().getSimpleName ().startsWith ("Valid" )) {
161+ Object hints = AnnotationUtils .getValue (ann );
161162 binder .validate (hints instanceof Object [] ? (Object []) hints : new Object [] {hints });
162163 break ;
163164 }
@@ -199,14 +200,13 @@ else if (this.annotationNotRequired) {
199200 * Add non-null return values to the {@link ModelAndViewContainer}.
200201 */
201202 @ Override
202- public void handleReturnValue (
203- Object returnValue , MethodParameter returnType ,
204- ModelAndViewContainer mavContainer , NativeWebRequest webRequest )
205- throws Exception {
203+ public void handleReturnValue (Object returnValue , MethodParameter returnType ,
204+ ModelAndViewContainer mavContainer , NativeWebRequest webRequest ) throws Exception {
206205
207206 if (returnValue != null ) {
208207 String name = ModelFactory .getNameForReturnValue (returnValue , returnType );
209208 mavContainer .addAttribute (name , returnValue );
210209 }
211210 }
211+
212212}
0 commit comments