Introduce alias for 'value' attribute in @RequestParam

Issue: SPR-11393
This commit is contained in:
Sam Brannen
2015-05-31 16:20:21 +02:00
parent 25a5d9d759
commit 034e0e2cf4
5 changed files with 39 additions and 25 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -23,10 +23,13 @@ import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.Map;
import org.springframework.core.annotation.AliasFor;
/**
* Annotation which indicates that a method parameter should be bound to a web
* request parameter. Supported for annotated handler methods in Servlet and
* Portlet environments.
* request parameter.
*
* <p>Supported for annotated handler methods in Servlet and Portlet environments.
*
* <p>If the method parameter type is {@link Map} and a request parameter name
* is specified, then the request parameter value is converted to a {@link Map}
@@ -39,6 +42,7 @@ import java.util.Map;
*
* @author Arjen Poutsma
* @author Juergen Hoeller
* @author Sam Brannen
* @since 2.5
* @see RequestMapping
* @see RequestHeader
@@ -53,10 +57,18 @@ import java.util.Map;
public @interface RequestParam {
/**
* The name of the request parameter to bind to.
* Alias for {@link #name}.
*/
@AliasFor(attribute = "name")
String value() default "";
/**
* The name of the request parameter to bind to.
* @since 4.2
*/
@AliasFor(attribute = "value")
String name() default "";
/**
* Whether the parameter is required.
* <p>Default is {@code true}, leading to an exception thrown in case

View File

@@ -260,7 +260,7 @@ public class HandlerMethodInvoker {
for (Annotation paramAnn : paramAnns) {
if (RequestParam.class.isInstance(paramAnn)) {
RequestParam requestParam = (RequestParam) paramAnn;
paramName = requestParam.value();
paramName = requestParam.name();
required = requestParam.required();
defaultValue = parseDefaultValueAttribute(requestParam.defaultValue());
annotationsFound++;
@@ -432,7 +432,7 @@ public class HandlerMethodInvoker {
for (Annotation paramAnn : paramAnns) {
if (RequestParam.class.isInstance(paramAnn)) {
RequestParam requestParam = (RequestParam) paramAnn;
paramName = requestParam.value();
paramName = requestParam.name();
paramRequired = requestParam.required();
paramDefaultValue = parseDefaultValueAttribute(requestParam.defaultValue());
break;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -49,10 +49,10 @@ public class RequestParamMapMethodArgumentResolver implements HandlerMethodArgum
@Override
public boolean supportsParameter(MethodParameter parameter) {
RequestParam ann = parameter.getParameterAnnotation(RequestParam.class);
if (ann != null) {
RequestParam requestParam = parameter.getParameterAnnotation(RequestParam.class);
if (requestParam != null) {
if (Map.class.isAssignableFrom(parameter.getParameterType())) {
return !StringUtils.hasText(ann.value());
return !StringUtils.hasText(requestParam.name());
}
}
return false;

View File

@@ -127,7 +127,7 @@ public class RequestParamMethodArgumentResolver extends AbstractNamedValueMethod
Class<?> paramType = parameter.getParameterType();
if (parameter.hasParameterAnnotation(RequestParam.class)) {
if (Map.class.isAssignableFrom(paramType)) {
String paramName = parameter.getParameterAnnotation(RequestParam.class).value();
String paramName = parameter.getParameterAnnotation(RequestParam.class).name();
return StringUtils.hasText(paramName);
}
else {
@@ -261,8 +261,8 @@ public class RequestParamMethodArgumentResolver extends AbstractNamedValueMethod
return;
}
RequestParam ann = parameter.getParameterAnnotation(RequestParam.class);
String name = (ann == null || StringUtils.isEmpty(ann.value()) ? parameter.getParameterName() : ann.value());
RequestParam requestParam = parameter.getParameterAnnotation(RequestParam.class);
String name = (requestParam == null || StringUtils.isEmpty(requestParam.name()) ? parameter.getParameterName() : requestParam.name());
if (value == null) {
builder.queryParam(name);
@@ -301,7 +301,7 @@ public class RequestParamMethodArgumentResolver extends AbstractNamedValueMethod
}
public RequestParamNamedValueInfo(RequestParam annotation) {
super(annotation.value(), annotation.required(), annotation.defaultValue());
super(annotation.name(), annotation.required(), annotation.defaultValue());
}
}