1
1
/*
2
- * Copyright 2002-2015 the original author or authors.
2
+ * Copyright 2002-2016 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.
37
37
import org .springframework .web .method .support .ModelAndViewContainer ;
38
38
39
39
/**
40
- * Resolves method arguments annotated with {@code @ModelAttribute} and handles
41
- * return values from methods annotated with {@code @ModelAttribute}.
40
+ * Resolve {@code @ModelAttribute} annotated method arguments and handle
41
+ * return values from {@code @ModelAttribute} annotated methods .
42
42
*
43
- * <p>Model attributes are obtained from the model or if not found possibly
44
- * created with a default constructor if it is available . Once created, the
45
- * attributed is populated with request data via data binding and also
46
- * validation may be applied if the argument is annotated with
47
- * {@code @javax. validation.Valid }.
43
+ * <p>Model attributes are obtained from the model or created with a default
44
+ * constructor (and then added to the model) . Once created the attribute is
45
+ * populated via data binding to Servlet request parameters. Validation may be
46
+ * applied if the argument is annotated with {@code @javax.validation.Valid}.
47
+ * or Spring's own {@code @org.springframework. validation.annotation.Validated }.
48
48
*
49
- * <p>When this handler is created with {@code annotationNotRequired=true},
49
+ * <p>When this handler is created with {@code annotationNotRequired=true}
50
50
* any non-simple type argument and return value is regarded as a model
51
51
* attribute with or without the presence of an {@code @ModelAttribute}.
52
52
*
53
53
* @author Rossen Stoyanchev
54
54
* @since 3.1
55
55
*/
56
- public class ModelAttributeMethodProcessor implements HandlerMethodArgumentResolver , HandlerMethodReturnValueHandler {
56
+ public class ModelAttributeMethodProcessor
57
+ implements HandlerMethodArgumentResolver , HandlerMethodReturnValueHandler {
57
58
58
59
protected final Log logger = LogFactory .getLog (getClass ());
59
60
60
61
private final boolean annotationNotRequired ;
61
62
62
63
63
64
/**
65
+ * Class constructor.
64
66
* @param annotationNotRequired if "true", non-simple method arguments and
65
67
* return values are considered model attributes with or without a
66
68
* {@code @ModelAttribute} annotation.
@@ -71,8 +73,9 @@ public ModelAttributeMethodProcessor(boolean annotationNotRequired) {
71
73
72
74
73
75
/**
74
- * Returns {@code true} if the parameter is annotated with {@link ModelAttribute}
75
- * or in default resolution mode, and also if it is not a simple type.
76
+ * Returns {@code true} if the parameter is annotated with
77
+ * {@link ModelAttribute} or, if in default resolution mode, for any
78
+ * method parameter that is not a simple type.
76
79
*/
77
80
@ Override
78
81
public boolean supportsParameter (MethodParameter parameter ) {
@@ -100,8 +103,8 @@ public final Object resolveArgument(MethodParameter parameter, ModelAndViewConta
100
103
NativeWebRequest webRequest , WebDataBinderFactory binderFactory ) throws Exception {
101
104
102
105
String name = ModelFactory .getNameForParameter (parameter );
103
- Object attribute = (mavContainer .containsAttribute (name ) ?
104
- mavContainer . getModel (). get ( name ) : createAttribute (name , parameter , binderFactory , webRequest ));
106
+ Object attribute = (mavContainer .containsAttribute (name ) ? mavContainer . getModel (). get ( name ) :
107
+ createAttribute (name , parameter , binderFactory , webRequest ));
105
108
106
109
WebDataBinder binder = binderFactory .createBinder (webRequest , attribute , name );
107
110
if (binder .getTarget () != null ) {
@@ -178,7 +181,8 @@ protected boolean isBindExceptionRequired(WebDataBinder binder, MethodParameter
178
181
179
182
/**
180
183
* Return {@code true} if there is a method-level {@code @ModelAttribute}
181
- * or if it is a non-simple type when {@code annotationNotRequired=true}.
184
+ * or, in default resolution mode, for any return value type that is not
185
+ * a simple type.
182
186
*/
183
187
public boolean supportsReturnType (MethodParameter returnType ) {
184
188
if (returnType .getMethodAnnotation (ModelAttribute .class ) != null ) {
0 commit comments