The fields of the list page require that they can be sorted according to the user's preferences, so each user's fields correspond to a different order (the field order is stored in the database). The values we take from the database are objects, but the foreground transfer values are ajax and json array, so we face the problem of object-to-json conversion: 1. The field order of each user is not fixed, and the code cannot be written dead. 2. Get the values according to the user's field order. If you use if to judge each value and then call a different method, there are too many if conditional statements. Then I looked at the reflection.
Model class, the same as normal model
public class Person { private String name; private int age; private String address; private String phoneNumber; private String sex; public String getName() { return name; }// The following are the get and set methods, omitted. } Test class
import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import java.util.ArrayList;import java.util.List;public class Test { // init person object. private Person initPerson() { Person p = new Person(); p.setName("name"); p.setAge(21); p.setAddress("this is my addrss"); p.setPhoneNumber("12312312312"); p.setSex("f"); return p; } public static void main(String[] args) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException { Test test = new Test(); Person p = test.initPerson(); List<String> list = new ArrayList<String>(); // Add all get method. // There is no '()' of methods name. list.add("getName"); list.add("getAge"); list.add("getAddress"); list.add("getPhoneNumber"); list.add("getSex"); for (String str : list) { // Get method instance. first param is method name and second param is param type. // Because Java exits the same method of different params, only method name and param type can confirm a method. Method method = p.getClass().getMethod(str, new Class[0]); // First param of invoke method is the object who calls this method. // Second param is the param. System.out.println(str + "(): Get Value is " + method.invoke(p, new Object[0])); } }}You can also use the fields obtained by the database to get the corresponding value from the object.
The above method needs to add a get method name to the list so that the value can be obtained based on the corresponding get method name. If the foreground is just a property name, then we have to convert it into the corresponding get method, which is troublesome.
public static void getValueByProperty(Person p, String propertyName) throws IntrospectionException, IllegalArgumentException, IllegalAccessException, InvocationTargetException { // get property by the argument propertyName. PropertyDescriptor pd = new PropertyDescriptor(propertyName, p.getClass()); Method method = pd.getReadMethod(); Object o = method.invoke(p); System.out.println("propertyName: " + propertyName + "/t value is: " + o); } public static void main(String[] args) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, IntrospectionException { Test test = new Test(); Person p = test.initPerson(); // get all properties. Field[] fields = p.getClass().getDeclaredFields(); for (Field field : fields) { getValueByProperty(p, field.getName()); } } In this way, you can directly obtain the corresponding value through the propertyName passed.
The above article Java reflection dynamically calls different methods (examples) according to different method names is all the content I share with you. I hope it can give you a reference and I hope you can support Wulin.com more.