Use of Java web-beanutils __HTML5

Source: Internet
Author: User
Tags java web

Beanutils is Apache is not satisfied with Sun's introspective class operation too troublesome so oneself developed a simple operation JavaBean attribute class-beanutils, all code is in NetEase cloud classroom see 30 days easy to master Javaweb download Beanutils development package, How to download online all have, because of the time reason, I did not introduce
The line should be optional, I did not try, if someone knows the words want to give me a message, thank you. First to build a javabean named person

Package com.test.beanutils;

Import java.util.Date;

public class Person {//javabean
    private String name;
    private String password;
    private int age;
    Private Date birthday;

    Public Date Getbirthday () {return
        birthday;
    }
    public void Setbirthday (Date birthday) {
        this.birthday = birthday;
    }
    Public String GetName () {return
        name;
    }
    public void SetName (String name) {
        this.name = name;
    }
    Public String GetPassword () {return
        password;
    }
    public void SetPassword (String password) {
        this.password = password;
    }
    public int getage () {return age
        ;
    }
    public void Setage (int age) {
        this.age = age;
    }

}
Using Beanutils operations JavaBean
Package com.test.beanutils;

Import java.lang.reflect.InvocationTargetException;
Import java.text.ParseException;
Import Java.text.SimpleDateFormat;
Import java.util.Date;
Import Java.util.HashMap;
Import Java.util.Map;

Import Org.apache.commons.beanutils.BeanUtils;
Import org.apache.commons.beanutils.ConversionException;
Import Org.apache.commons.beanutils.ConvertUtils;
Import Org.apache.commons.beanutils.Converter;
Import Org.apache.commons.beanutils.locale.converters.DateLocaleConverter;
Import Org.junit.Test;

public class Demo1 {
}

This is the general of the entire class, because to write a comment for each method, you take it out and write the following method into the Demo1 class

@Test public
    void Test1 () throws Illegalaccessexception, invocationtargetexception {person
        p = new person (); C16/>beanutils.setproperty (P, "name", "Nia");
        System.out.println (P.getname ());
    }

SetProperty has three parameters, the first is the object to be operated JavaBean, the second is a property name inside the JavaBean object, and the third is the value we need to assign to that attribute;

@Test public
    void Test2 () throws Illegalaccessexception, invocationtargetexception {person
        p = new Person (); 
  string name = "ABCD";
        String password = "1234";
        String age = "n";

        Beanutils bu = new Beanutils ();
        Beanutils.setproperty (P, "name", name);
        Bu.setproperty (P, ' age ', age); Age is of type string, but the age of person is type int
                                       //buanutils automatically type conversions, but beanutils only supports
                                       8 types of//java base (such as int, double , etc.);
        Bu.setproperty (P, "password", password)

        ; System.out.println (P.getname () + "   " + p.getpassword () + "  " + p.getage ());

    }

This method indicates that the Beanutils assignment is converted automatically, but only the underlying type.

Let Beanutils convert the 8 major types of the base, (such as data) @Test public void Test3 () throws Illegalaccessexception, InvocationTargetException
        {Person p = new person ();
        String name = "ABCD";
        String password = "1234";
        String age = "23";

        String birthday = "1996-1-26";  Registers a type converter convertutils.register (new Converter () {@Override public Object convert (Class
                Type, Object value {if (value = = null) {return null; } if (! (
                Value instanceof String)) {throw new Conversionexception ("LZ only supports string type conversions");
                String str = (string) value;
                if (Str.trim (). Equals ("")) {return null;

                } SimpleDateFormat df = new SimpleDateFormat ("Yyyy-mm-dd");
                try {return df.parse (str);
       catch (ParseException e) {            throw new RuntimeException (e);

        }}, Date.class);
        Beanutils.setproperty (P, "name", name); 
        Beanutils.setproperty (P, ' age ', age);
        Beanutils.setproperty (P, "password", password);

        Beanutils.setproperty (P, "Birthday", birthday);
        System.out.println (P.getname () + "" + P.getpassword () + "" + p.getage ());
    System.out.println (P.getbirthday ());
 }

This method is a description of how beanutils can be converted to a type other than the 8 major types, such as the date type.
To register a converter first, use an anonymous class. The first parameter in Convertutils.register is the new Convertor object, and the process of convertor transformation is changed. The second parameter is the type that needs to be transformed, such as the above is need to convert to Date,so write Date.class;

@Test public
    void Test4 () throws Illegalaccessexception, invocationtargetexception {person
        p = new Person (); 
  string name = "AAAA";
        String password = "1234";
        String age = "n";
        String birthday = "1996-12-24";

        Convertutils.register (New Datelocaleconverter (), date.class);

        Beanutils.setproperty (P, "name", name);
        Beanutils.setproperty (P, ' age ', age);
        Beanutils.setproperty (P, "password", password);
        Beanutils.setproperty (P, "Birthday", birthday);

        System.out.println (P.getname () + "   " + p.getpassword () + "  " + p.getage ());
        Date date = P.getbirthday ();
        System.out.println (Date.tolocalestring ());

    }

This approach is to register beanutils with the conversion of other types of converters, but this one bug is if the String birthday = "1996-12-24" above; This statement becomes a String birthday = ""; This statement, when run error, but test3 to us will not.

@Test public
    void Test5 () throws Illegalaccessexception, InvocationTargetException {
        map<string, String > map = new hashmap<string, string> ();
        Map.put ("name", "bbbb");
        Map.put ("Password", "2334");
        Map.put ("Age", "a");
        Map.put ("Birthday", "1234-12-3");  

        Convertutils.register (New Datelocaleconverter (), date.class);
        Person bean = new person ();  
        Beanutils.populate (bean, map); Populates the Bean's properties with the map collection
        System.out.println (Bean.getname ());
        System.out.println (Bean.getpassword ());
        System.out.println (Bean.getage ());
        System.out.println (Bean.getbirthday ());
    }

When using this method, it is important to note that the key of the map must have the same name as the attribute in JavaBean.

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.