나는 PropertyEditor가 SpringMVC에 의해 제공되었다고 생각했지만 오늘은 이것이 JDK가 제공한다는 것을 알았습니다.
이 일은 무엇을 할 수 있습니까? 문자열을 Bean 객체로 변환 할 수 있습니다. Spring MVC는 양식 양식을 컨트롤러의 매개 변수 개체에 매핑 할 때이 제품을 사용합니다.
예를 참조하십시오 :
Nodedo.java : 표준 Javabean 객체
공개 클래스 NodeDo {개인 문자열 이름; 개인 문자열 이메일; 개인 날짜; 공개 문자열 getName () {return name;} public void setName (문자열 이름) {this.name = name;} public string getEmail () {return 이메일;} public void seteMail (문자열 이메일) {this.email = 이메일;} public setDate (} public setDate) {this.date = date;}@attrementpublic string toString () {return "nodedo [name =" + name + ", email =" + email + ", date =" + dodedoeditor.sdf.sdf.format (date) + "];}}testdo.java와 마찬가지로 Nodedo는 다음과 같습니다.
공개 클래스 testdo {private String nodename; 개인 Nodedo Nodedo; 공개 문자열 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) 먼저, 속성을 변환하는 데 사용되는 Property Erditor를 정의해야합니다.
Public Class DodedoEditor 확장 PropertyEditorSupport {public static final simpledateformat sdf = new SimpledateFormat ( "yyyy-mm-dd hh : mm : ss"); @override public void settasext (문자열 텍스트)는 불법적 인 날려서 {string [] tokens = text.split ( "// |"); Nodedo Nodedo = New Nodedo (); nodedo.setName (토큰 [0]); Nodedo.SetEmail (토큰 [1]); try {nodedo.setdate (sdf.parse (토큰 [2]); } catch (parseException e) {새로운 불법 행위 exception (e); } setValue (nodedo); }}(2) 변환 :
public class propertyeditorsample {static {propertyeditormanager.registereditor (nodedo.class, dodedoeditor.class); } 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 ()); } private static testdo convert (map <string, string> parameters)는 예외 {testdo testdo = new testdo (); beaninfo bi = introspector.getBeanInfo (testdo.class); PropertyDescriptor [] pds = bi.getPropertyDescriptors (); for (propertyDescriptor pd : pds) {class <?> propertyType = pd.getPropertype (); 메소드 writemethod = pd.getWritemEthod (); if (propertytype == class.class) {// excore} else if (propertyType == string.class) {writeMethod.invoke (testdo, parameters.get (pd.getName ())); } else {propertyEditor editor = PropertyEditorManager.finditor (PropertyType); if (editor! = null) {editor.setastext (parameters.get (pd.getName ())); WritemEthod.invoke (testdo, editor.getValue ()); } else {system.out.println ( "편집기 없음 :"+pd.getName ()); }} return testdo; }}}실제로 두 가지 핵심 사항 만 있습니다
<pre name = "code"> editor.setastext (parameters.get (pd.getname ())); // 1 editor.getValue (); // 2 // 따라서 settastext에서 호출되어 변환 된 값을 저장하여 getValue ()를 통해 얻을 수 있습니다.
위의 내용은 이번에 컴파일 된 모든 컨텐츠입니다. 여전히 이해하지 못하는 것이 있다면 아래에 메시지를 남겨서 논의 할 수 있습니다. wulin.com을 지원 해주셔서 감사합니다.