I used to think that PropertyEditor was provided by SpringMVC, but today I found out that this was provided by jdk, which is amazing!
What can this thing do? You can convert a string into a bean object. Spring mvc uses this thing when mapping form form to controller's parameter object.
See an example:
NodeDO.java: A standard javabean object
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(String email) {this.email = email;}public Date getDate() {return date;}public void setDate(Date date) {this.date = date;}@Overridepublic String toString() {return "NodeDO [name=" + name + ", email=" + email + ", date=" + DodeDOEditor.sdf.format(date) + "]";}}Like TestDO.java, NodeDO is referenced:
public class TestDO { private String nodeName; private NodeDO nodeDO; 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; } }We want to implement a function like this:
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(parameters); System.out.println(testDo.getNodeName()); System.out.println(testDo.getNodeDO()); } How to convert parameters map into TestDO object?
(1) First, you need to define a PropertyEditor used to convert Property:
public class DodeDOEditor extends PropertyEditorSupport{ public static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); @Override public void setAsText(String text) throws 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){ throw new IllegalArgumentException(e); } setValue(nodeDo); } }(2) Convert:
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(parameters); 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(); Method writeMethod = pd.getWriteMethod(); if(propertyType == Class.class){ //ignore }else if(propertyType == String.class){ writeMethod.invoke(testDO, parameters.get(pd.getName())); }else{ PropertyEditor editor = PropertyEditorManager.findEditor(propertyType); if(editor != null){ editor.setAsText(parameters.get(pd.getName())); writeMethod.invoke(testDO, editor.getValue()); }else{ System.out.println("no editor for:"+pd.getName()); } } return testDO; } } }In fact, there are only two key points
<pre name="code">editor.setAsText(parameters.get(pd.getName()));//1 editor.getValue();//2 // Therefore, usually setValue() is called in setAsText to save the converted value, so that you can get it through getValue()
The above is all the content compiled this time. If you still have anything you don’t understand, you can leave a message below to discuss. Thank you for your support for Wulin.com.