Java Reflection-mapping form values to model __java

Source: Internet
Author: User
Tags reflection in domain

Recall the long history of programming life Road, can not help but feel sad, perhaps this is the first time in my life more formal use of reflection (/ω╲), not nonsense, see the code:

Import java.lang.reflect.InvocationTargetException;
Import Java.lang.reflect.Method;
Import java.util.ArrayList;
Import java.util.Enumeration;
Import Java.util.HashMap;
Import java.util.List;
Import Java.util.Map;
Import Javax.servlet.http.HttpServletRequest;

Import org.nutz.lang.Strings; /** * @author Ai Xiaotian * need to return the Object strong into a first pass in model on the line * * public class Formutil {private static final string string = "java.lang.String";//for type judgement in reflection private static final String INTEGER = "Java.lang.Integer";//for type judgement in reflection private s Tatic final String BOOLEAN = "Java.lang.Boolean";//for type judgement in reflection private static final String domain = "domain.";/ /form the uniform prefix of each field, ps:domain.id private static final String SET = "set";//To get model SET method, assign value to property/** * * @ PARAM request * @param target: The model class that needs to be mapped * @return/public static Object Getdomainmap (HttpServlet
        Request request, Object target) {map<string, object> dr = new Hashmap<> (); BeenTake all the forms in domain.
        The field value that begins with the enumeration<string> e = Request.getparameternames () in the Dr.
            while (E.hasmoreelements ()) {String paramname = e.nextelement (); if (! Strings.isblank (paramname) && Paramname.startswith (DOMAIN) {dr.put paramname.substring (7). Touppe
            Rcase (), Request.getparameter (paramname)); //Get all set** methods in model list<method> Methodarr = GetSetMethod (Target.getclass (). GetName (
        ));
            For (method Method:methodarr) {String propertname = method.getname (). substring (3). toUpperCase ();
        By traversing all set methods, the value is passed to the corresponding property SetParam (method, Dr.get (propertname), target);
    return target; /** * Get all methods with set** in model; * @param pkgname: Class full path name * @return/@SuppressWarnings ({"R Awtypes "," Unchecked "}) private static list<method> GetSetMethod (String pkgname) {list<method>Methodarr = new ArrayList ();
           try {Class clazz = Class.forName (pkgname);
           Method[] methods = Clazz.getmethods ();
               For (method Method:methods) {String methodname = Method.getname ();
               if (Methodname.indexof (SET) = = 0) {Methodarr.add (method);
       A catch (ClassNotFoundException e) {e.printstacktrace ());
   return Methodarr; /** * Call the method in model to pass the value to the corresponding property * @param method:model method * @param value: corresponding set * @param target: The model class that needs to be mapped/private static void SetParam (method method, object value, Object target) {class<?>[
       ] Parametertypes = Method.getparametertypes ();
       There is generally only one parameter, directly taking [0] the String paramtype = Parametertypes[0].getname (); if (! Strings.isblank (paramtype) && value!= null &&! "". Equals (value)) {The try {//model corresponds to only these three types of data in the form.There is a need to add switch (paramtype) {case STRING:method.invoke (target, VA
                       Lue.tostring ());

                   Break
                       Case INTEGER:method.invoke (Target, New INTEGER (Value.tostring ()));

                   Break
                       Case BOOLEAN:method.invoke (Target, New BOOLEAN (Value.tostring ()));

                   Break
                       Default:method.invoke (Target, value.tostring ());
               Break catch (Illegalaccessexception | IllegalArgumentException |
            InvocationTargetException e) {e.printstacktrace ();; }
       }
   }

}

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.