Spring MVC binding parameters

Source: Internet
Author: User

Spring MVC binding parameters
I. Simple binding of JavaBean

Suppose we have a simple Person class.

public class Person {private String name;private Integer age;public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}}

Then how can we bind the request parameters to the Person object?

1. First, we need to expose the object to the parameters of the ing method.

@RequestMapping("/test1")public void test1(Person person, HttpServletResponse response)throws IOException {System.out.println(Utils.parseJson(person));sendJson(response, "");}

Note: Utils. parseJson (...) is a method that I encapsulate to convert json, just to view the output

It is worth noting that this person object will never be null. If no parameter is assigned to the property of person, then person is equivalent to a new Person (), and all attributes are default values.

2. Then, use the following request http: // localhost: 8080/Mobile/test1? Name = zhu & age = 10 to bind the parameter to the person object.


2. bind to a simple array
@RequestMapping("/test1")public void test1(String[] ids, HttpServletResponse response)throws IOException {System.out.println(Utils.parseJson(ids));sendJson(response, "");}

Note: If the parameter does not contain ids, this ids is null. Otherwise, ids are automatically separated into arrays based on "," by default.

For example, the foreground sends the following request: http: // localhost: 8080/Mobile // test1? Ids = a, B, c


3. bind to List

This binding is not the same as the original idea of 80%. You need to bind a "helper class" to the List. You cannot bind the List directly in the RequestMapping method parameter (as I know)

Next we will use the Group class to bind a Group of person objects.

public class Group {private List
 
   personList;public List
  
    getPersonList() {return personList;}public void setPersonList(List
   
     personList) {this.personList = personList;}}
   
  
 

In this case, the request format is http: // localhost: 8080/Mobile/test1? PersonList [0]. name = zhu & personList [1]. name = zhao


4. bind to Map

You also need to use the "auxiliary class"

public class Group {private Map
 
   paramMap;public Map
  
    getParamMap() {return paramMap;}public void setParamMap(Map
   
     paramMap) {this.paramMap = paramMap;}}
   
  
 

In this case, the request format is http: // localhost: 8080/Mobile/test1? ParamMap [age] = 12 & paramMap [name] = zhu

Parameters must be in the format of paramMap [age], while paramMap. age cannot be bound to map.


5. bind to "rich object"

First, explain what is "rich object". That is to say, the object has a JavaBean attribute.

public class Group {private Person leader;private List
 
   members;public Person getLeader() {return leader;}public void setLeader(Person leader) {this.leader = leader;}public List
  
    getMembers() {return members;}public void setMembers(List
   
     members) {this.members = members;}}
   
  
 

Now that we know how to bind to members, it's easy to bind to leader.

Only: http: // localhost: 8080/Mobile/test1? Leader. name = zhu & leader. age = 20


Here we only briefly introduce some common binding usage. If you want to really master the principles, you still need to study them well ~ For example, BeanWrapper and PropertyEditor

Custom Editor to expand more parameter binding methods



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.