SpringMVC: submit parameter names and parameter names reception problems

1, the domain name and submit the same processing method parameter names

Submission of data:  HTTP: // localhost :? 8080/111 the Hello name =

Approach :

@RequestMapping("/hello")
public String hello(String name){
    System.out.println(name);
    return "hello";
}

Background output: 111

 

2, domain name and submit the inconsistent approach of parameter names

Submission of data:  HTTP: // localhost :? 8080/222 the Hello username =

//@RequestParam("username") : username提交的域的名称 .
@RequestMapping("/hello")
public String hello(@RequestParam("username") String name){
    System.out.println(name);
    return "hello";
}

Background output: 222

3, submitted by an object

Unanimously requested the attribute name form fields and objects, you can use the object parameter

  1. Entity class

    public class User {
        private int id;
        private String name;
        private int age;
        //构造
        //get/set
        //tostring()
    }

     

  2. Submission of data:  HTTP: // localhost : 8080 / mvc04 / the User name = 111 & the above mentioned id = 2 & Age = 13?
  3. Approach :

    @RequestMapping("/user")
    public String user(User user){
        System.out.println(user);
        return "hello";
    }

     

    Background Output: User {id = 2, name = '111', age = 13}

Note: If you use an object, then pass the front of the parameter name and the object name must be consistent, otherwise it is null.

This article is for learning notes, referring to https://blog.kuangstudy.com/index.php/archives/476/ the blogger, please contact tort delete!

Guess you like

Origin www.cnblogs.com/GOOGnine/p/12186570.html