2012-01-04 24 views
0

似たようないくつかの質問がありますが、まだこの問題を解決する答えが見つかりません。限り、今Spring MVC - 空白のフォームの値をプリミティブなBeanフィールドにバインドする

<form:form action="count.do"> 
    <form:input id="counter" path="counter"/> 
</form:form> 

:私は(明確にするために略記)を有する

public class MyBean { 
    private int counter; 

    public int getCounter() { 
    return counter; 
    } 

    public void setCounter(int counter) { 
    this.counter = counter; 
    } 
} 

JSPフォームで:私は原始的性質を使用してコマンド・オブジェクトによってバックアップされたスプリングMVC形を有しますそこに有効な数値は、すべてが期待どおりに動作しますが、「カウンタ」の入力欄にありますが、フィールドが空白の場合、私は、フォームの検証エラーを取得:

org.springframework.validation.BeanPropertyBindingResult: 1 errors Field error in object 'MyBean' on field 'counter': rejected value []; codes [methodInvocation.MyBean.counter,methodInvocation.counter,methodInvocation.float,methodInvocation]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [MyBean.counter,counter]; arguments []; default message [counter]]; default message [Property 'counter' threw exception; nested exception is java.lang.IllegalArgumentException] 

私はカスタムプロパティエディタを作成しようとしましたにNULL値を受け入れる(initBinder()でそれを初期化)が、私はラッパーアクセサの非プリミティブセット作成した場合、これが唯一の作品:だから

binder.registerCustomEditor(Integer.class, new CustomNumberEditor(Integer.class, true)); 

を、フォーム上のフィールドをバインドする方法があります非プリミティブラッパーを作成せずにプリミティブ型を使用し、空白の値を処理します(エラーにするのではなく0に設定したいと思います)。もしそうなら、簡単な例を投稿してもよろしいですか?

多くのおかげで、 ピーター

答えて

0

は、私はそれをしようと試みていないが、int.class(これ本当に作品)のためのエディタを登録しようとしません。

しかし、intが処理できないため、nullnew CustomNumberEditor(Integer.class, true)は機能しません。

したがって、たとえば0を返す独自の数値エ​​ディタが必要です。値が定義されていません。

binder.registerCustomEditor(int.class, 
    new MyNumberEditorThatConvertsNullToZero()); 
関連する問題