Tool class for encapsulating request parameters using the Getparametermap () method

Source: Internet
Author: User

We know that the GetParameter (name) of the HttpServletRequest class, Getparametervalues (name), can implement individual arguments to the page and accept multiple parameters of the same name, respectively. In particular getparameter (name) This method is used more often. Sometimes the page comes with multiple parameters, to write more than one request.getparameter (name) to accept, of course, under the SPRINGMVC framework you can choose to use the object to accept parameters, you can also on the method to receive the parameters individually. The downside to this is that you need to create different objects that accept parameters, or accept them on your method (the code looks ugly if you have multiple arguments), in the case of requests for parameters and name changes. The purpose of this article is to use HttpServletRequest's Getparametermap () method to encapsulate the request parameters, directly below the code.

1 ImportJava.util.HashMap;2 ImportJava.util.Iterator;3 ImportJava.util.Map;4 ImportJava.util.Map.Entry;5 6 Importjavax.servlet.http.HttpServletRequest;7 8  Public classRequestparamstomap {9The return value type is map<string, object>Ten      Public StaticMap<string, object>Getparametermap (HttpServletRequest request) { OneMap<string, string[]> properties =Request.getparametermap ();//Package request parameters into Map<string, string[]> Amap<string, object> returnmap =NewHashmap<string, object>(); -Iterator<entry<string, string[]>> iter =Properties.entryset (). iterator (); -String name = ""; theString value = ""; -          while(Iter.hasnext ()) { -entry<string, string[]> Entry =Iter.next (); -Name =Entry.getkey (); +Object valueobj =Entry.getvalue (); -             if(NULL==valueobj) { +Value = ""; A}Else if(Valueobjinstanceofstring[]) { atString[] values =(string[]) valueobj; -                  for(inti = 0; i < values.length; i++{//For multiple identical names in request parameters -Value = Values[i] + ","; -                 } -Value = value.substring (0, Value.length ()-1); -}Else { inValue =valueobj.tostring ();// used to request parameter names unique in request parameters  -             } to returnmap.put (name, value); +         } -         returnReturnmap; the     } *The return value type is map<string, string> $      Public StaticMap<string, string>Getparameterstringmap (HttpServletRequest request) {Panax NotoginsengMap<string, string[]> properties =Request.getparametermap (); //Package request parameters into Map<string, string[]>  -map<string, string> returnmap =NewHashmap<string, string>(); theString name = ""; +String value = ""; A          for(Map.entry<string, string[]>Entry:properties.entrySet ()) { theName =Entry.getkey (); +String[] values =Entry.getvalue (); -             if(NULL==values) { $Value = ""; $}Else if(values.length>1) { -                  for(inti = 0; i < values.length; i++{ //For multiple identical names in request parameters  -Value = Values[i] + ","; the                 } -Value = value.substring (0, Value.length ()-1);Wuyi}Else { theValue = Values[0]; // used to request parameter names unique in request parameters   -             } Wu returnmap.put (name, value); -              About         } $         returnReturnmap; -     } -      -}

The code is so much, of course, if you want to convert the resulting map collection into a Bean object, you can use the import Org.apache.commons.beanutils.BeanUtils; class Beanutils.populate (Bean, map ) method to convert the map to the bean, which is automatically matched based on the key value of the map and the attributes of the Bean object (at this point, the first method is recommended).

Tool class for encapsulating request parameters using the Getparametermap () method

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.