Java EE------------class reflection directly encapsulates the parameters passed by the foreground.

Source: Internet
Author: User

In the Java EE Web, we collect data from the form table only son, usually in the form of a value object, to get, encapsulate, and then upload the value object to the background for use.

Such as:

<span style= "FONT-SIZE:18PX;" >                String name=request.getparameter ("name"); String pwd=request.getparameter ("pwd"); String ID =request.getparameter ("id"); User User =new User (); User.setuid (ID); user.setname (name); User.setpwd (PWD);</span>
It's been a lot of trouble every time, and now we have new technology .

This can be achieved with both packages.

<span style= "FONT-SIZE:18PX;" >public void Login (HttpServletRequest req, HttpServletResponse resp) throws Servletexception, IOException {map< String, string[]> map=req.getparametermap (); User user = Beanutils.populate (user.class, map); System.out.println (user);</span>

In the servlet we just need to call to do, in the user value object, our foreground parameters can be set, there is no need to ignore.

But we use this need to note: front page code <input type= "text" name= "name"/> This parameter must be aligned, and the value object to be consistent, otherwise it will not succeed, Because in class reflection, we all get the name of the variable, and then we call the set+ variable name one capitalization to set.

<span style= "FONT-SIZE:18PX;" >public class Beanutils {public static <t>t populate (T t,map<string,string[]> Map) {try{ Org.apache.commons.beanutils.BeanUtils.populate (T,MAP); return t;} catch (Exception e) {throw new RuntimeException (E.getmessage (), E);}} public static <t>t populate (class<t> cls,map<string,string[]> Map) {try{t T = cls.newinstance (); Return populate (t, map);} catch (Exception e) {throw new RuntimeException (E.getmessage (), E);}} </span>
abovewith two packets, just pass the corresponding parameter, and we can package it accordingly (in fact, it is the class reflection) .

Two packages.


Java EE------------class reflection directly encapsulates the parameters passed by the foreground.

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.