How spring MVC Passes collection parameters (list, array)

Source: Internet
Author: User

Spring MVC can automatically help you encapsulate parameters to become objects, do not have to manually through the request to get a parameter, but such an automatic parameter encapsulation touches the set parameters may require a little bit of skill to do.

What is the difference between an underlying type and a reference type?

The underlying type is stored directly on the stack, the reference type (object) value is stored on the heap, the address is stored on top of the stack, and the underlying type has a non-null default value, such as int default is 0,boolean default is false, reference type unless it is to open new space with new, Otherwise, only the address information has no value information. The difference between int and integer is not only the problem of having no default value, but the integer also has its own member variable, although it is static, such as Integer. Max_value is one of them, and the value of integer is the member variable that is stored in an address constant value.

Second, the base type, the assignment time does not need to specifically named the member variable (String, the wrapper class of the underlying type) of the reference type collection parameters

The main thing about the difference between the underlying type and the reference type is that the underlying type is not a member variable, the underlying type can be directly a=x, and the value of the reference type is mostly a.b=x to get, so it is easiest to get the collection parameters of the underlying type. For example, if you have a method that requires an array of int or a list of lists, it can be done directly on the page with the input box of the same name.

<input type= "text" value= "" Name= "TitleName" id= "Add_titlename"/><input type= "text" value= "" Name= "TitleName "Id=" Add_titlename "/><input type=" text "value=" "Name=" TitleName "id=" Add_titlename "/>

Background access

public void Getparmas (list<string> titlename)

Or

public void Getparmas (string[] titlename)

Can get the value to TitleName.

Here, string is not a 8 base type, but there is a common feature of string and underlying type, which is that when assigning a value, you do not need to specifically nominate the member variable, including the wrapper class of the underlying type.

When assigning a value, you do not need to specifically nominate a collection parameter of a member variable reference type

These classes are basically our own set of business logic class, in many cases also contains a combination of relationships, first of all, the first case, simply receive a list<?> xxx;

  

@RequestMapping ("/add") public    void Add (Model model,list<sysrole> sysrole) {     ....    }

This way is no, will be reported Could not instantiate Bean class [java.util.List]: Specified class was an interface this exception.

 for class class Interface  classclassinterface

How did it work out?

We need to use the second case.

Receive an object with a list member variable, such as you have a class is user, a user can have more than one contact phone,

class user{   List<Contact> contactlist;   }

We want to get a user parameter with multiple contacts

 Public void addUser (user user) {...}

On the page can be directly through

<type= "text"  value= "" "  name=" user.  Contactlist[0].phone"  ID=" add_contact_phone_1 "/>

This is the way to get the parameters.

So what do we do with the first situation? Turn it into a second way to handle it, meaning that a new wrapper class is specifically packaged to wrap a similar list parameter that needs to be received.

Why is the first way out of this?

Because the way the parameters are obtained in spring MVC no matter how many, his nature remains

Request.getparameter ("name")

That encapsulates this parameter into an object and only the same setter method, the key to the problem is how to find the setter
Method? It must be the name in the SetName and the name in the request. To find it. You want to, if you simply receive a list parameter, list has get and set method, but there is no name Ah, only according to the array subscript to determine the parameter position. So we can only pass the parameter by the second method.

How spring MVC Passes collection parameters (list, array)

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.