私は以前はPropertyeditorがSpringMVCによって提供されたと思っていましたが、今日、これがJDKによって提供されていることがわかりました。
これは何ができますか?文字列をBeanオブジェクトに変換できます。 Spring MVCは、フォームをコントローラーのパラメーターオブジェクトにマッピングするときにこのことを使用します。
例を参照してください:
nodedo.java:標準のJavabeanオブジェクト
public class nodedo {private string name; private string email; private date date; public string getname(){return name;} public void setname(string name){this.name = name;} public string getemail(){return email;} public void setemail(} public void setemail( {this.date = date;}@overridepublic string toString(){return "nodedo [name =" + name + "、email =" + email + "、date =" + dodedoeditor.sdf.format(date) + "]";}}}}testdo.javaのように、ノデドが参照されます。
パブリッククラスのtestdo {private string nodename;プライベートノデドノデド; public string getNodename(){return nodename; } public void setNodename(string nodename){this.nodename = nodename; } public nodedo getnodedo(){return nodedo; } public void setnodedo(nodedo nodedo){this.nodedo = nodedo; }}このような関数を実装したい:
public static void main(string [] args)スロー例外{map <string、string> parameters = new hashmap <string、string>(){{put( "nodename"、 "xiaopang test"); put( "nodedo"、 "xiaopang| [email protected]|2015-10-20 12:00:00"); }}; testDo testDo = convert(パラメーター); System.out.println(testdo.getNodename()); System.out.println(testdo.getNodedo()); }パラメーターマップをTestDoオブジェクトに変換する方法は?
(1)最初に、プロパティを変換するために使用されるプロパティエディタを定義する必要があります。
Public Class Dodedoeditorは、PropertyEditorSupportを拡張します{public static final simpledateformat sdf = new simpledateFormat( "yyyy-mm-dd hh:mm:ss"); @Override public void setastext(string text)shrows IllegalargumentException {string [] tokens = text.split( "// |"); nodedo nodedo = new nodedo(); nodedo.setname(tokens [0]); nodedo.setemail(tokens [1]); try {nodedo.setdate(sdf.parse(tokens [2])); } catch(parseexception e){新しいIllegalargumentException(e); } setValue(nodedo); }}(2)変換:
public class propertyeditorsample {static {propertyeditormanager.registereditor(nodedo.class、dodedoeditor.class); } public static void main(string [] args)throws exception {map <string、string> parameters = new hashmap <string、string>(){{put( "nodename"、 "xiaopang test"); put( "nodedo"、 "xiaopang| [email protected]|2015-10-20 12:00:00"); }}; testDo testDo = convert(パラメーター); System.out.println(testdo.getNodename()); System.out.println(testdo.getNodedo()); } private static testdo convert(map <string、string> parameters)throws exception {testdo testdo = new testdo(); beaninfo bi = introspector.getbeaninfo(testdo.class); propertyDescriptor [] PDS = bi.getPropertyDescriptors(); for(propertydescriptor pd:pds){class <?> propertyType = pd.getPropertyType();メソッドwritemethod = pd.getwritemethod(); if(propertyType == class.class){// adganore} else if(propertytype == string.class){writemethod.invoke(testdo、parameters.get(pd.getname())); } else {propertyeditor editor = propertyeditormanager.fidenditor(propertyType); if(editor!= null){editor.setastext(parameters.get(pd.getname())); writemethod.invoke(testdo、editor.getvalue()); } else {system.out.println( "編集者なし:"+pd.getName()); }} return testdo; }}}実際、重要なポイントは2つしかありません
<pre name = "code"> editor.setastext(parameters.get(pd.getname())); // 1 editor.getValue(); // 2 //したがって、setAstextでsetastextで呼び出されるため、getValue(
上記は、今回コンパイルされたすべてのコンテンツです。まだわからないものがある場合は、以下にメッセージを残して議論することができます。 wulin.comへのご支援ありがとうございます。