Skip to content

Commit d170846

Browse files
committed
Log message for unused @ModelAttribute name
Closes gh-23877
1 parent 66bb145 commit d170846

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

spring-web/src/main/java/org/springframework/web/method/annotation/ModelFactory.java

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 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.
@@ -24,6 +24,9 @@
2424
import java.util.Map;
2525
import java.util.Set;
2626

27+
import org.apache.commons.logging.Log;
28+
import org.apache.commons.logging.LogFactory;
29+
2730
import org.springframework.beans.BeanUtils;
2831
import org.springframework.core.Conventions;
2932
import org.springframework.core.GenericTypeResolver;
@@ -58,6 +61,9 @@
5861
*/
5962
public final class ModelFactory {
6063

64+
private static final Log logger = LogFactory.getLog(ModelFactory.class);
65+
66+
6167
private final List<ModelMethod> modelMethods = new ArrayList<>();
6268

6369
private final WebDataBinderFactory dataBinderFactory;
@@ -135,14 +141,22 @@ private void invokeModelAttributeMethods(NativeWebRequest request, ModelAndViewC
135141
}
136142

137143
Object returnValue = modelMethod.invokeForRequest(request, container);
138-
if (!modelMethod.isVoid()){
139-
String returnValueName = getNameForReturnValue(returnValue, modelMethod.getReturnType());
140-
if (!ann.binding()) {
141-
container.setBindingDisabled(returnValueName);
142-
}
143-
if (!container.containsAttribute(returnValueName)) {
144-
container.addAttribute(returnValueName, returnValue);
144+
if (modelMethod.isVoid()) {
145+
if (StringUtils.hasText(ann.value())) {
146+
if (logger.isDebugEnabled()) {
147+
logger.debug("Name in @ModelAttribute is ignored because method returns void: " +
148+
modelMethod.getShortLogMessage());
149+
}
145150
}
151+
continue;
152+
}
153+
154+
String returnValueName = getNameForReturnValue(returnValue, modelMethod.getReturnType());
155+
if (!ann.binding()) {
156+
container.setBindingDisabled(returnValueName);
157+
}
158+
if (!container.containsAttribute(returnValueName)) {
159+
container.addAttribute(returnValueName, returnValue);
146160
}
147161
}
148162
}

0 commit comments

Comments
 (0)