Support BindParam annotation
Allows customizing the name of the request parameter to bind a constructor parameter to. Closes gh-30947
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* Copyright 2002-2018 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://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.
|
||||
*/
|
||||
|
||||
package org.springframework.web.bind.annotation;
|
||||
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* Annotation to bind values from a web request such as request parameters or
|
||||
* path variables to fields of a Java object. Supported on constructor parameters
|
||||
* of {@link ModelAttribute @ModelAttribute} controller method arguments
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
* @since 6.1
|
||||
* @see org.springframework.web.bind.WebDataBinder#construct
|
||||
*/
|
||||
@Target(ElementType.PARAMETER)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
public @interface BindParam {
|
||||
|
||||
/**
|
||||
* The lookup name to use for the bind value.
|
||||
*/
|
||||
String value() default "";
|
||||
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* Copyright 2002-2018 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://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.
|
||||
*/
|
||||
|
||||
package org.springframework.web.bind.support;
|
||||
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.validation.DataBinder;
|
||||
import org.springframework.web.bind.annotation.BindParam;
|
||||
|
||||
/**
|
||||
* {@link org.springframework.validation.DataBinder.NameResolver} that determines
|
||||
* the bind value name from a {@link BindParam @BindParam} method parameter
|
||||
* annotation.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
* @since 6.1
|
||||
*/
|
||||
public final class BindParamNameResolver implements DataBinder.NameResolver {
|
||||
|
||||
@Override
|
||||
public String resolveName(MethodParameter parameter) {
|
||||
BindParam bindParam = parameter.getParameterAnnotation(BindParam.class);
|
||||
if (bindParam != null) {
|
||||
if (StringUtils.hasText(bindParam.value())) {
|
||||
return bindParam.value();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -91,6 +91,7 @@ public class DefaultDataBinderFactory implements WebDataBinderFactory {
|
||||
@Nullable ResolvableType type) throws Exception {
|
||||
|
||||
WebDataBinder dataBinder = createBinderInstance(target, objectName, webRequest);
|
||||
dataBinder.setNameResolver(new BindParamNameResolver());
|
||||
|
||||
if (target == null && type != null) {
|
||||
dataBinder.setTargetType(type);
|
||||
@@ -99,6 +100,7 @@ public class DefaultDataBinderFactory implements WebDataBinderFactory {
|
||||
if (this.initializer != null) {
|
||||
this.initializer.initBinder(dataBinder);
|
||||
}
|
||||
|
||||
initBinder(dataBinder, webRequest);
|
||||
|
||||
if (this.methodValidationApplicable && type != null) {
|
||||
@@ -112,7 +114,7 @@ public class DefaultDataBinderFactory implements WebDataBinderFactory {
|
||||
|
||||
/**
|
||||
* Extension point to create the WebDataBinder instance.
|
||||
* By default this is {@code WebRequestDataBinder}.
|
||||
* By default, this is {@code WebRequestDataBinder}.
|
||||
* @param target the binding target or {@code null} for type conversion only
|
||||
* @param objectName the binding target object name
|
||||
* @param webRequest the current request
|
||||
|
||||
Reference in New Issue
Block a user