アンダースコアで始まるリクエスト パラメータをアンダースコアのないフィールドにマップするServletRequestDataBinderのカスタム サブクラスを作成してみてください。次に、カスタマイズされたServletRequestDataBinderFactoryを作成してサブクラスを作成する必要があります。次に、 RequestMappingHandlerAdapterのカスタム サブクラスを作成し、Spring MVC に登録する必要があります。これにより、POJO フィールド名に必ずしもアンダースコアを必要とせずに、HTML でアンダースコアを使用し続けることができます。
カスタム ServletRequestDataBinder:
public class MyCustomServletRequestDataBinder extends
ExtendedServletRequestDataBinder {
public MyCustomServletRequestDataBinder(Object target) {
super(target);
}
public MyCustomServletRequestDataBinder(Object target, String objectName) {
super(target, objectName);
}
@Override
protected void addBindValues(MutablePropertyValues mpvs,
ServletRequest request) {
super.addBindValues(mpvs, request);
addUnderscoreBindValues(mpvs, request);
}
protected void addUnderscoreBindValues(MutablePropertyValues mpvs,
ServletRequest request) {
// go through each parameter and check if it starts with an underscore (_)
// if it starts with an underscore, add it to mpvs under the name without
// the underscore
@SuppressWarnings("unchecked")
Map<String, Object> parameterMap = request.getParameterMap();
for (Map.Entry<String, Object> entry : parameterMap.entrySet()) {
if (StringUtils.startsWith(entry.getKey(), "_")) {
mpvs.add(StringUtils.removeStart(entry.getKey(), "_"), entry.getValue());
}
}
}
}
カスタム ServletRequestDataBinderFactory:
public class MyCustomServletRequestDataBinderFactory extends
ServletRequestDataBinderFactory {
/**
* Create a new instance.
* @param binderMethods one or more {@code @InitBinder} methods
* @param initializer provides global data binder initialization
*/
public MyCustomServletRequestDataBinderFactory(
List<InvocableHandlerMethod> binderMethods,
WebBindingInitializer initializer) {
super(binderMethods, initializer);
}
/**
* Returns an instance of {@link MyCustomServletRequestDataBinder}.
*/
@Override
protected ServletRequestDataBinder createBinderInstance(Object target,
String objectName, NativeWebRequest request) {
return new MyCustomServletRequestDataBinder(target, objectName);
}
}
カスタム RequestMappingHandlerAdapter:
public class MyCustomRequestMappingHandlerAdapter extends
RequestMappingHandlerAdapter {
public MyCustomRequestMappingHandlerAdapter() {
super();
}
/**
* {@inheritDoc} Creates an instance of
* {@link MyCustomServletRequestDataBinderFactory}.
*/
@Override
protected ServletRequestDataBinderFactory createDataBinderFactory(
List<InvocableHandlerMethod> binderMethods) throws Exception {
return new MyCustomServletRequestDataBinderFactory(binderMethods,
getWebBindingInitializer());
}
}
次に、カスタム HandlerAdapter を登録します。
@Configuration
@Import(ValidationConfiguration.class)
@ComponentScan(basePackageClasses = ControllerScanMarker.class)
public class MyCustomHandlerConfig extends WebMvcConfigurationSupport {
/**
* {@inheritDoc} Returns the subclass
* {@link MyCustomRequestMappingHandlerAdapter} in place of the default
* {@link RequestMappingHandlerAdapter}.
*/
@Override
@Bean
public RequestMappingHandlerAdapter requestMappingHandlerAdapter() {
...
RequestMappingHandlerAdapter adapter = new MyCustomRequestMappingHandlerAdapter();
// set additional adapter fields here...
...
return adapter;
}
}
デフォルトのリクエストパラメーターバインディング中に値を変更/追加する簡単な方法があると確信していますが、他のフックがどこにあるか思い出せません。