ModelAttributeMethodProcessor.java 8.4 KB
Newer Older
1
/*
2
 * Copyright 2002-2015 the original author or authors.
3 4 5 6 7 8 9 10 11 12 13 14 15 16
 *
 * 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.
 */

17
package org.springframework.web.method.annotation;
18 19

import java.lang.annotation.Annotation;
20
import java.util.Map;
21

22 23
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
24

25 26
import org.springframework.beans.BeanUtils;
import org.springframework.core.MethodParameter;
27
import org.springframework.core.annotation.AnnotationUtils;
28 29
import org.springframework.validation.BindException;
import org.springframework.validation.Errors;
30
import org.springframework.validation.annotation.Validated;
31 32 33 34 35 36 37 38 39 40
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.bind.support.WebRequestDataBinder;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.method.support.ModelAndViewContainer;

/**
41 42
 * Resolves method arguments annotated with {@code @ModelAttribute} and handles
 * return values from methods annotated with {@code @ModelAttribute}.
43 44 45 46 47
 *
 * <p>Model attributes are obtained from the model or if not found possibly
 * created with a default constructor if it is available. Once created, the
 * attributed is populated with request data via data binding and also
 * validation may be applied if the argument is annotated with
48
 * {@code @javax.validation.Valid}.
49
 *
50 51
 * <p>When this handler is created with {@code annotationNotRequired=true},
 * any non-simple type argument and return value is regarded as a model
52
 * attribute with or without the presence of an {@code @ModelAttribute}.
53
 *
54 55 56
 * @author Rossen Stoyanchev
 * @since 3.1
 */
R
Rossen Stoyanchev 已提交
57
public class ModelAttributeMethodProcessor implements HandlerMethodArgumentResolver, HandlerMethodReturnValueHandler {
58

59
	protected final Log logger = LogFactory.getLog(getClass());
60

61
	private final boolean annotationNotRequired;
62

63

64
	/**
65
	 * @param annotationNotRequired if "true", non-simple method arguments and
66
	 * return values are considered model attributes with or without a
67
	 * {@code @ModelAttribute} annotation.
68
	 */
69 70
	public ModelAttributeMethodProcessor(boolean annotationNotRequired) {
		this.annotationNotRequired = annotationNotRequired;
71 72
	}

73

74
	/**
75 76
	 * Returns {@code true} if the parameter is annotated with {@link ModelAttribute}
	 * or in default resolution mode, and also if it is not a simple type.
77
	 */
78
	@Override
79 80 81 82
	public boolean supportsParameter(MethodParameter parameter) {
		if (parameter.hasParameterAnnotation(ModelAttribute.class)) {
			return true;
		}
83
		else if (this.annotationNotRequired) {
84 85 86 87 88 89 90 91
			return !BeanUtils.isSimpleProperty(parameter.getParameterType());
		}
		else {
			return false;
		}
	}

	/**
92 93
	 * Resolve the argument from the model or if not found instantiate it with
	 * its default if it is available. The model attribute is then populated
94 95 96 97 98
	 * with request values via data binding and optionally validated
	 * if {@code @java.validation.Valid} is present on the argument.
	 * @throws BindException if data binding and validation result in an error
	 * and the next method parameter is not of type {@link Errors}.
	 * @throws Exception if WebDataBinder initialization fails.
99
	 */
100
	@Override
101 102
	public final Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,
			NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
103

104
		String name = ModelFactory.getNameForParameter(parameter);
105 106
		Object attribute = (mavContainer.containsAttribute(name) ?
				mavContainer.getModel().get(name) : createAttribute(name, parameter, binderFactory, webRequest));
107

108
		WebDataBinder binder = binderFactory.createBinder(webRequest, attribute, name);
109
		if (binder.getTarget() != null) {
110
			bindRequestParameters(binder, webRequest);
111
			validateIfApplicable(binder, parameter);
112 113 114 115
			if (binder.getBindingResult().hasErrors()) {
				if (isBindExceptionRequired(binder, parameter)) {
					throw new BindException(binder.getBindingResult());
				}
116 117 118
			}
		}

119 120 121 122 123 124
		// Add resolved attribute and BindingResult at the end of the model

		Map<String, Object> bindingResultModel = binder.getBindingResult().getModel();
		mavContainer.removeAttributes(bindingResultModel);
		mavContainer.addAllAttributes(bindingResultModel);

125
		return binder.convertIfNecessary(binder.getTarget(), parameter.getParameterType(), parameter);
126 127 128
	}

	/**
129 130
	 * Extension point to create the model attribute if not found in the model.
	 * The default implementation uses the default constructor.
131
	 * @param attributeName the name of the attribute (never {@code null})
132 133
	 * @param parameter the method parameter
	 * @param binderFactory for creating WebDataBinder instance
134
	 * @param request the current request
135
	 * @return the created model attribute (never {@code null})
136
	 */
137 138 139
	protected Object createAttribute(String attributeName, MethodParameter parameter,
			WebDataBinderFactory binderFactory,  NativeWebRequest request) throws Exception {

140
		return BeanUtils.instantiateClass(parameter.getParameterType());
141
	}
142

143
	/**
144 145
	 * Extension point to bind the request to the target object.
	 * @param binder the data binder instance to use for the binding
146 147
	 * @param request the current request
	 */
148
	protected void bindRequestParameters(WebDataBinder binder, NativeWebRequest request) {
149 150 151 152
		((WebRequestDataBinder) binder).bind(request);
	}

	/**
153
	 * Validate the model attribute if applicable.
154 155 156
	 * <p>The default implementation checks for {@code @javax.validation.Valid},
	 * Spring's {@link org.springframework.validation.annotation.Validated},
	 * and custom annotations whose name starts with "Valid".
157 158
	 * @param binder the DataBinder to be used
	 * @param parameter the method parameter
159
	 */
160
	protected void validateIfApplicable(WebDataBinder binder, MethodParameter parameter) {
161
		Annotation[] annotations = parameter.getParameterAnnotations();
162
		for (Annotation ann : annotations) {
163 164 165 166 167
			Validated validatedAnn = AnnotationUtils.getAnnotation(ann, Validated.class);
			if (validatedAnn != null || ann.annotationType().getSimpleName().startsWith("Valid")) {
				Object hints = (validatedAnn != null ? validatedAnn.value() : AnnotationUtils.getValue(ann));
				Object[] validationHints = (hints instanceof Object[] ? (Object[]) hints : new Object[] {hints});
				binder.validate(validationHints);
168
				break;
169 170 171 172 173
			}
		}
	}

	/**
174
	 * Whether to raise a {@link BindException} on validation errors.
175 176
	 * @param binder the data binder used to perform data binding
	 * @param parameter the method argument
177
	 * @return {@code true} if the next method argument is not of type {@link Errors}.
178
	 */
179
	protected boolean isBindExceptionRequired(WebDataBinder binder, MethodParameter parameter) {
180 181 182
		int i = parameter.getParameterIndex();
		Class<?>[] paramTypes = parameter.getMethod().getParameterTypes();
		boolean hasBindingResult = (paramTypes.length > (i + 1) && Errors.class.isAssignableFrom(paramTypes[i + 1]));
183

184 185 186
		return !hasBindingResult;
	}

187
	/**
188
	 * Return {@code true} if there is a method-level {@code @ModelAttribute}
189 190
	 * or if it is a non-simple type when {@code annotationNotRequired=true}.
	 */
191
	@Override
192
	public boolean supportsReturnType(MethodParameter returnType) {
193 194 195 196 197 198 199 200 201
		if (returnType.getMethodAnnotation(ModelAttribute.class) != null) {
			return true;
		}
		else if (this.annotationNotRequired) {
			return !BeanUtils.isSimpleProperty(returnType.getParameterType());
		}
		else {
			return false;
		}
202 203
	}

204 205 206
	/**
	 * Add non-null return values to the {@link ModelAndViewContainer}.
	 */
207
	@Override
208 209
	public void handleReturnValue(Object returnValue, MethodParameter returnType,
			ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception {
210

211 212
		if (returnValue != null) {
			String name = ModelFactory.getNameForReturnValue(returnValue, returnType);
213
			mavContainer.addAttribute(name, returnValue);
214
		}
215
	}
216

217
}