Beanwrapper and PropertyEditor in Springframework

Source: Internet
Author: User
Tags dateformat

Beanwrapper is an excuse under the Org.springframework.beans package, the corresponding implementation class is Beanwrapperimpl, provides the corresponding Get/set method, and sets the readability and the writable property.

public class Company {private String    Name    private Employee managingdirector; public String getName () {return this.na Me } public void setName (String name) { THIS.name = name; } public Employee getmanagingdirector () {return this.managingdirector; } public void setmanagingdirector (Employee managingdirector) { Span class= "Hl-keyword" >this.managingdirector = Managingdirector; }} 
Public class Employee { private String name; private float salary; Public String GetName () { return this.name;} public void SetName (String name) { this.name = name;} Public Float getsalary () { return salary;} public void Setsalary (float salary) { this.salary = salary;}}
Beanwrapper Company =New Beanwrapperimpl (New company ());Setting the company Name..company.setpropertyvalue ("Name","Some Company Inc.");... can also be do like this:propertyvalue value =New PropertyValue ("Name","Some Company Inc."); Company.setpropertyvalue (value);OK, let's create the director and tie it to the company:beanwrapper Jim =New Beanwrapperimpl (New Employee ()); Jim.setpropertyvalue ("Name","Jim Stravinsky"); Company.setpropertyvalue ( "Managingdirector", Jim.getwrappedinstance ()); //retrieving the salary of the managingdirector through the companyfloat salary = (Float) compan Y.getpropertyvalue ( "managingdirector.salary");
From the above code, Beanwrapper can be seen as the parent type after POJOs is transformed into a bean.

Spring introduces the concept of PropertyEditor, primarily for the purpose of implementing string types and other types of conversions. The most common is the conversion of string types and date types, and Customdateeditor is a specialized property editor for Java.util.Data
that supports commonly used dataformat. In the actual operation often encountered in the form of date strings and JavaBean in the date type of the properties of the automatic conversion, and SPRINGMVC default does not support the conversion of this format, so must be
manually configured, custom data type binding to achieve this function. It is relatively simple to apply SPRINGMVC annotations @initbinder and spring-brought Webdatabinder classes and operations directly.
@InitBinder
public void Initbinder (Webdatabinder binder) {
SimpleDateFormat DateFormat = new SimpleDateFormat ("Yyyy-mm-dd");
Dateformat.setlenient (FALSE);
Binder.registercustomeditor (Date.class, New Customdateeditor (DateFormat, true));
}



             

Beanwrapper and PropertyEditor in Springframework

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.