Java param values are automatically stored in the bean

Source: Internet
Author: User

Transferred from: http://www.2cto.com/kf/201402/278960.html

This article assumes that the parameter name is consistent with the entity attribute!

Java Web project, often have the servlet in the receive parameter assignment to the entity, if the parameter is more cumbersome, here are two simple and common methods:

For example, the entity is student

1. Use org.apache.commons.beanutils.BeanUtils:

  
New  Student (); Beanutils.populate (Stu, Request.getparametermap ());

Of course, the actual work, some parameter value is empty or the parameter type does not match the entity attribute type, this will report the exception, so this is not very reliable, then you can use Method 3

2. Using Reflection:

Get all the set methods, exclude the Set method of the object base class itself, get the property name from the Get method, and the property name is the parameter name of the request

3. (most recommended, tested) modified populate method:

ImportJava.lang.reflect.Method;Importjava.util.Date;Importjavax.servlet.http.HttpServletRequest;Importorg.apache.commons.lang3.ArrayUtils;Importorg.apache.commons.lang3.StringUtils;Importorg.apache.commons.lang3.math.NumberUtils;Importorg.apache.commons.lang3.time.DateUtils;/**entity tools in the servlet business *@authorliuding * 2014-2-16-PM 08:10:06*/ Public classServletbeantools {/**Auto-Match parameter assignment to entity Bean *@authorliuding * 2014-2-16 pm 10:23:37 *@paramBean *@paramRequest*/     Public Static voidPopulate (Object bean, httpservletrequest request) {Class<!--?extendsObject--> Clazz =Bean.getclass (); Method ms[]=Clazz.getdeclaredmethods ();        String Mname;        String field;        String FieldType;        String value;  for(Method m:ms) {mname=M.getname (); if(!mname.startswith ("Set")                    ||Arrayutils.isempty (M.getparametertypes ())) {                Continue; }            Try{field= Mname.tolowercase (). CharAt (3) + mname.substring (4, Mname.length ()); Value=request.getparameter (field); if(Stringutils.isempty (value)) {Continue; } FieldType= M.getparametertypes () [0].getname (); //The following can confirm that value is of type string                if(String.class. GetName (). Equals (FieldType))                {M.invoke (bean, (String) value); }Else if(Integer.class. GetName (). Equals (FieldType) &&numberutils.isdigits ((String) value))                {M.invoke (Bean, integer.valueof (String) value); }Else if(short.)class. GetName (). Equals (FieldType) &&numberutils.isdigits ((String) value))                {M.invoke (Bean, short.valueof (String) value); }Else if(Float.class. GetName (). Equals (FieldType) &&Numberutils.isnumber ((String) value))                {M.invoke (Bean, float.valueof (String) value); }Else if(Double.class. GetName (). Equals (FieldType) &&Numberutils.isnumber ((String) value))                {M.invoke (Bean, double.valueof (String) value); }Else if(Date.class. GetName (). Equals (FieldType)) {M.invoke (Bean, dateutils.parsedate (String) value,"Yyyy-mm-dd", "Yyyy-mm-dd HH:mm:ss")); }Else{M.invoke (bean, value); }            }Catch(Exception e) {e.printstacktrace (); Continue; }        }    }}

Java param values are automatically stored in the bean

Java param values are automatically stored in the bean

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.