大概了解了propertyEditor的工作方式,主要關(guān)注convert string to object
記下來,省得用到時再亂找
步聚:
1,寫出你要轉(zhuǎn)換的bean及對應(yīng)的propertyEditor(如:Person=>PersonEditor)
2,用PropertyEditorManager注冊:
java代碼: |
PropertyEditorManager.registerEditor(Person.class,PersonEditor.class) |
3,你得到一個要轉(zhuǎn)換的string,查出想轉(zhuǎn)換成的bean對映的propertyEditor
如:
java代碼: |
PropertyEditorManager.findEditor(Person.class) |
4,你知道啦,調(diào)用你找到的editor的setAsText(String)
如:
java代碼: |
editor.setAsText("23,liunix"); |
5,在你屬性宿主bean上調(diào)用setter
如:
java代碼: |
personManager.setPerson((Person)editor.getValue()) |
另外,可以省去PropertyEditorManager.registerEditor注冊方法有
1,
你要轉(zhuǎn)換的bean及對應(yīng)的propertyEditor在同一個包內(nèi),而且名字形如:Person=>Person+Editor來定義兩個類的名字,
2,名稱對應(yīng),不在一個包內(nèi)
調(diào)用PropertyEditorManager.setEditorSearchPath(String[])
其中string[]指定你editor的包名即可