2011-10-17 7 views
0

私はArrayListフィールドでDTO(豆)を持っている:私は私のinitBinderでSpring MVCバインディング:ArrayListをバインドする方法<...>?

public MyDTO { 
    ... 
    private List<MyThing> things; 
    ... 
    ... getters, setters and so on 
} 

@InitBinder 
public void initBinder(WebDataBinder binder) { 
    ... 
    binder.registerCustomEditor(List.class, "things", new PropertyEditorSupport() { 
    @Override 
    public void setAsText(String text) throws IllegalArgumentException { 
     List<MyThing> things = new ArrayList<MyThings>; 

     // fill things array with data from text 
     ... 


     // On that stage things value is correct! 
     super.setValue(things); 
    } 
    }); 
} 

そして、私のコントローラ要求メソッドで:

@RequestMapping({"save"}) 
public ModelAndView doSaveMyDTO(@ModelAttribute MyDTO myDTO) { 
    // very strange myDTO comes here=(
} 

問題は私がregisterCustomEditorのスタッフにいる間にthingsの配列が大丈夫だということです。

しかし、私はdoSaveMyDTO方法を取得 - MyDTO.thingsは、実際の値の1つの素子配列の配列のようになります。

期待

(initBinder中のもの):

[value1, value2, value3] 

は(doSaveMyDTOでmyDTO.getThingsを取得します()):

[[value1], [value2], [value3]] 

なぜですか?要求が正しく形成されている場合は...

答えて

2

を説明(things=v1&things=v2&things=v3またはthings=v1,v2,v3)、春の内蔵コンバータは適切Listに変換するべきではありませんしてください - あなた自身を登録する必要が。

あなたの入力がJSONである場合は、@RequestBody代わりの@ModelAttribute

+0

残念ながら私は、JSON文字列として私のリストを持っている...とにかく、あなたのアドバイスに感謝を必要とするだろう - 私はそれで作ってみるよをあなたの方法... – leshka

+0

ああ、json別のものです。あなたのjsonはどのように見えるのですか?そしてそれについては、あなたには多くの必要があると思います@RequestBody – Bozho

関連する問題