cc.shacocloud.mirage.web.bind.ConfigurableWebBindingInitializer Maven / Gradle / Ivy
package cc.shacocloud.mirage.web.bind;
import lombok.Getter;
import lombok.Setter;
import org.springframework.core.convert.ConversionService;
import org.jetbrains.annotations.Nullable;
import org.springframework.validation.BindingErrorProcessor;
import org.springframework.validation.MessageCodesResolver;
import org.springframework.validation.Validator;
/**
* 方便的{@link WebBindingInitializer}用于在应用程序上下文中进行声明式配置。
*
* 允许与多个控制器/处理程序一起重用预配置的初始化程序。
*
* @see #setDirectFieldAccess
* @see #setMessageCodesResolver
* @see #setBindingErrorProcessor
* @see #setValidator(Validator)
* @see #setConversionService(ConversionService)
*/
public class ConfigurableWebBindingInitializer implements WebBindingInitializer {
/**
* 设置活页夹是否应尝试自动增长包含空值的嵌套路径。
*
* @see org.springframework.validation.DataBinder#initBeanPropertyAccess()
* @see org.springframework.validation.DataBinder#setAutoGrowNestedPaths
*/
@Setter
@Getter
private boolean autoGrowNestedPaths = true;
/**
* 设置是否使用直接字段访问而不是bean属性访问
*
*
* @see org.springframework.validation.DataBinder#initDirectFieldAccess()
* @see org.springframework.validation.DataBinder#initBeanPropertyAccess()
*/
@Setter
@Getter
private boolean directFieldAccess = false;
/**
* 设置用于将错误解决为消息代码的策略。
* 将给定策略应用于此控制器使用的所有数据绑定器。
*
* 默认值为{@code null},即使用数据绑定程序的默认策略。
*
* @see org.springframework.validation.DataBinder#setMessageCodesResolver
*/
@Setter
@Getter
@Nullable
private MessageCodesResolver messageCodesResolver;
/**
* 设置用于处理绑定错误的策略,即:必填字段错误和{@code PropertyAccessException}。
*
* 默认值为{@code null},即使用数据绑定程序的默认策略。
*
* @see org.springframework.validation.DataBinder#setBindingErrorProcessor
*/
@Setter
@Getter
@Nullable
private BindingErrorProcessor bindingErrorProcessor;
@Setter
@Getter
@Nullable
private Validator validator;
/**
* 指定一个转换服务,该转换服务将应用于每个DataBinder
*/
@Setter
@Getter
@Nullable
private ConversionService conversionService;
@Override
public void initBinder(WebDataBinder binder) {
binder.setAutoGrowNestedPaths(this.autoGrowNestedPaths);
if (this.directFieldAccess) {
binder.initDirectFieldAccess();
}
if (this.messageCodesResolver != null) {
binder.setMessageCodesResolver(this.messageCodesResolver);
}
if (this.bindingErrorProcessor != null) {
binder.setBindingErrorProcessor(this.bindingErrorProcessor);
}
if (this.validator != null && binder.getTarget() != null &&
this.validator.supports(binder.getTarget().getClass())) {
binder.setValidator(this.validator);
}
if (this.conversionService != null) {
binder.setConversionService(this.conversionService);
}
}
}