Spring BeanUtils component used in JSP development, springbeanutils

Source: Internet
Author: User

Spring BeanUtils component used in JSP development, springbeanutils

Use of Spring BeanUtils components in JSP development

Javabean used for demonstration

import java.util.Date;public class People {  private String name;  private int age;  private Date birth;  public People(String name, int age, Date birth) {    super();    this.name = name;    this.age = age;    this.birth = birth;  }  public People() {    super();    // TODO Auto-generated constructor stub  }  @Override  public String toString() {    return "People [name=" + name + ", age=" + age + ", birth=" + birth + "]";  }  public String getName() {    return name;  }  public void setName(String name) {    this.name = name;  }  public int getAge() {    return age;  }  public void setAge(int age) {    this.age = age;  }  public Date getBirth() {    return birth;  }  public void setBirth(Date birth) {    this.birth = birth;  }}

Test (all tests are only related to the source javabean attribute value and the target javabean attribute value)

Replication of the target an attribute when the source javabean attribute has values

@Testpublic void springBeanUtilsTest(){  People oldPeople = new People("oldName",100,new Date());  People newPeople = new People();  //BeanUtils.copyProperties(Object source,Object target);    BeanUtils.copyProperties(oldPeople, newPeople);  System.out.println(oldPeople);  System.out.println(newPeople);}

The output result is as follows:

People [name=oldName, age=100, birth=Wed Jul 19 18:46:13 CST 2017]People [name=oldName, age=100, birth=Wed Jul 19 18:46:13 CST 2017]

Replication of the target an attribute when the attribute value of the source javabean non-Date type is null

@Testpublic void springBeanUtilsTest(){  People oldPeople = new People(null,100,new Date());  People newPeople = new People("newName",20,null);  //BeanUtils.copyProperties(Object source,Object target);    BeanUtils.copyProperties(oldPeople, newPeople);  System.out.println(oldPeople);  System.out.println(newPeople);}

The output result is as follows:

Note: The non-null attribute value in the target javabean is overwritten with null.

People [name=null, age=100, birth=Wed Jul 19 19:04:48 CST 2017]People [name=null, age=100, birth=Wed Jul 19 19:04:48 CST 2017]

Copy of the attribute values in the target javabean when the value of the Date type in the source javabean is null

@Testpublic void springBeanUtilsTest(){  People oldPeople = new People("oldName",100,null);  People newPeople = new People("newName",20,new Date());  //BeanUtils.copyProperties(Object source,Object target);    BeanUtils.copyProperties(oldPeople, newPeople);  System.out.println(oldPeople);  System.out.println(newPeople);}

The output result is as follows:

People [name=oldName, age=100, birth=null]People [name=oldName, age=100, birth=null]

BeanUtils. copyProperties (Object source, Object target); there is a deficiency in the method, that is, when the attribute value corresponding to the source attribute is null, it will also overwrite the attribute of the same attribute name in the target, even if the attribute value in the target already exists and is not null, this is obviously unreasonable, this is an overload method that we can use:

BeanUtils.copyProperties(Object source,Object target, String... ignoreProperties);

The meaning of the last parameter is the attribute name ignored when copying the attribute value. You only need to find the array of attribute names whose attribute value is null in source. The method is as follows:

/***** @ Title: getNullPropertyNames * @ Description: obtain an array of attribute names * @ param source * @ return */public static String [] getNullPropertyNames (Object source) whose attribute value is null in an Object) {final BeanWrapper src = new BeanWrapperImpl (source); java. beans. propertyDescriptor [] pds = src. getPropertyDescriptors (); Set <String> emptyNames = new HashSet <String> (); for (java. beans. propertyDescriptor pd: PPS) {Object srcValue = src. getPropertyValue (pd. getName (); if (srcValue = null) emptyNames. add (pd. getName ();} String [] result = new String [emptyNames. size ()]; return emptyNames. toArray (result );}

Test

@Testpublic void copyBeanNotNull(){  People oldPeople = new People(null, 100, null);  People newPeople = new People("newName", 20, new Date());  //BeanUtils.copyProperties(Object source,Object target, String... ignoreProperties);    BeanUtils.copyProperties(oldPeople, newPeople, getNullPropertyNames(oldPeople));  System.out.println(oldPeople);  System.out.println(newPeople);  for(String key : getNullPropertyNames(oldPeople)){    System.out.println(key);  }}

The output result is as follows:

People [name=null, age=100, birth=null]People [name=newName, age=100, birth=Wed Jul 19 23:31:05 CST 2017]namebirth

The above is the use of Spring BeanUtils components in JSP. If you have any questions, please leave a message or go to the community on this site for discussion. Thank you for reading this article and hope to help you. Thank you for your support for this site!

Related Article

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.