私のカスタムタイプにしましょう(これは Immutable Value Object であるため、セッターがないことに注意してください):
class CustomType extends ValueObject {
private String value;
@NotNull
public String getValue();
CustomType(String value) { this.value = value;}
CustomType(String prefix, String suffix) { this.value = prefix + suffix;}
public String getPrefix() { return value.substring(0, 4);}
public String getSuffix() { return value.substring(4);}
}
そして私のコントローラー:
@Controller
class MyController {
public ModelAndView processSubmittedForm(@Valid CustomType myObject, BindingResult result) {..}
}
そして私のjspビューフォーム:
<form>
<input id="prefixField" name="prefix" value="756." type="hidden">
<input id="suffixField" name="suffix" type="text">
...
<form/>
このビューが2 つの POST パラメータprefix
とsuffix
myObject
を送信することを考えると、null 以外の値で Spring によって検証されると仮定して、これら 2 つのパラメータを単一のオブジェクトにバインドするにはどうすればよい でしょうか?
WebDataBinder、InitBinder をカスタマイズしたり、Formatter を登録したり、カスタム エディターなどを使用したりして、これを実現できますか?
ご助力ありがとうございます。
編集:これは、私自身の問題に一致する解決策を1つも見つけずにグーグルで検索した関連記事です:
- http://forum.springsource.org/showthread.php?130618-Custom-HandlerMethodArgumentResolver-that-uses-messageConverters
- Spring MVC カスタマイズされたメソッド パラメーター バインディング
- Spring 部分更新オブジェクトのデータ バインディング
- http://sergialmar.wordpress.com/2011/03/29/extending-handler-method-argument-resolution-in-spring-mvc/
- @PathVariable のようなカスタム WebArgumentResolver