SpringのBooleanの値のマッピング

SpringだとHTTP requestで送られてきた値をBooleanにマッピングする場合
true、on、yes、1ならtrueに、false、off、no、0だったらfalseに、値がなかったらnullに、それ以外の値であったらサーバーとして値を受けつけないという動作をしてくれるらしい。*1

HTMLのcheckboxではチェックが入っていたら値を送る、チェックが入っていなかったら値を送らないという動作をしてくれるだけなので、相性が悪い。
なので、PropertyEditorを拡張してBooleanの値の取り扱い方を変更してあげる。

大まかな流れととしては、以下のブログを参照していただいて、
http://jyukutyo.hatenablog.com/entry/20100512/1273742460

BooleanEditorを以下の用に作成。

import java.beans.PropertyEditorSupport;
public class BooleanEditor extends PropertyEditorSupport {
    @Override
    public void setAsText(String text) throws IllegalArgumentException {
        this.setValue(text != null);
    }
}

先のブログに記載してある方法でWebBindingInitializerの実装クラスを作成して登録してあげればよい。
なんかいまいちだなー。

[twitter:@making]さんから突っ込みが入りましたが、Spring3.2以降は以下のやり方の方がよいようです。
http://terasolunaorg.github.io/guideline/public_review/ImplementationAtEachLayer/ApplicationLayer.html#controlleradvice