1
1
/*
2
- * Copyright 2002-2012 the original author or authors.
2
+ * Copyright 2002-2014 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
21
21
22
22
import org .apache .commons .logging .Log ;
23
23
import org .apache .commons .logging .LogFactory ;
24
+
24
25
import org .springframework .beans .BeanUtils ;
25
26
import org .springframework .core .MethodParameter ;
26
27
import org .springframework .core .annotation .AnnotationUtils ;
@@ -58,6 +59,7 @@ public class ModelAttributeMethodProcessor implements HandlerMethodArgumentResol
58
59
59
60
private final boolean annotationNotRequired ;
60
61
62
+
61
63
/**
62
64
* @param annotationNotRequired if "true", non-simple method arguments and
63
65
* return values are considered model attributes with or without a
@@ -67,6 +69,7 @@ public ModelAttributeMethodProcessor(boolean annotationNotRequired) {
67
69
this .annotationNotRequired = annotationNotRequired ;
68
70
}
69
71
72
+
70
73
/**
71
74
* @return true if the parameter is annotated with {@link ModelAttribute}
72
75
* or in default resolution mode also if it is not a simple type.
@@ -94,18 +97,16 @@ else if (this.annotationNotRequired) {
94
97
* @throws Exception if WebDataBinder initialization fails.
95
98
*/
96
99
@ 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 {
101
102
102
103
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 ) );
105
106
106
- WebDataBinder binder = binderFactory .createBinder (request , attribute , name );
107
+ WebDataBinder binder = binderFactory .createBinder (webRequest , attribute , name );
107
108
if (binder .getTarget () != null ) {
108
- bindRequestParameters (binder , request );
109
+ bindRequestParameters (binder , webRequest );
109
110
validateIfApplicable (binder , parameter );
110
111
if (binder .getBindingResult ().hasErrors ()) {
111
112
if (isBindExceptionRequired (binder , parameter )) {
@@ -120,17 +121,17 @@ public final Object resolveArgument(
120
121
mavContainer .removeAttributes (bindingResultModel );
121
122
mavContainer .addAllAttributes (bindingResultModel );
122
123
123
- return binder .getTarget ();
124
+ return binder .convertIfNecessary ( binder . getTarget (), parameter . getParameterType (), parameter );
124
125
}
125
126
126
127
/**
127
128
* Extension point to create the model attribute if not found in the model.
128
129
* 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})
130
131
* @param parameter the method parameter
131
132
* @param binderFactory for creating WebDataBinder instance
132
133
* @param request the current request
133
- * @return the created model attribute, never {@code null}
134
+ * @return the created model attribute ( never {@code null})
134
135
*/
135
136
protected Object createAttribute (String attributeName , MethodParameter parameter ,
136
137
WebDataBinderFactory binderFactory , NativeWebRequest request ) throws Exception {
@@ -155,9 +156,9 @@ protected void bindRequestParameters(WebDataBinder binder, NativeWebRequest requ
155
156
*/
156
157
protected void validateIfApplicable (WebDataBinder binder , MethodParameter parameter ) {
157
158
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 );
161
162
binder .validate (hints instanceof Object [] ? (Object []) hints : new Object [] {hints });
162
163
break ;
163
164
}
@@ -199,14 +200,13 @@ else if (this.annotationNotRequired) {
199
200
* Add non-null return values to the {@link ModelAndViewContainer}.
200
201
*/
201
202
@ 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 {
206
205
207
206
if (returnValue != null ) {
208
207
String name = ModelFactory .getNameForReturnValue (returnValue , returnType );
209
208
mavContainer .addAttribute (name , returnValue );
210
209
}
211
210
}
211
+
212
212
}
0 commit comments