Spring MVC framework - Common Annotations

 

  • @RequestMapping  

    Spring MVC by @RequestMapping annotation request service mapping method, the method is defined in the class definition can be added to the annotation.

    Common parameters:

      1, value: Specifies the physical address of the request, the default value of @ RequestMapping;

      2, method: specifies the type of request, GET, POST, PUT, DELETE and the like;

      3, params: parameter must contain the designation request;

 

  • @RequestMapping

    Parameter binding:

      1, declare the parameter list when the service method definitions;

      2, to add parameters @RequestParam notes;   

        Traditional 
          http: // localhost:? 7777 / hello / index id = 10 & name = a
@RequestMapping(value = "/index",method = RequestMethod.GET,params = {"id=10","name"})
public String index(@RequestParam("id") int num,@RequestParam("name") String gender){
  System.out.println(num);
  System.out.println(gender);
  return "index";
}
          = the params { "id = 10", "name" }
            specify parameter values, the parameter id must pass 10;
          @RequestParam ( "id") int NUM, @ RequestParam ( "name") Gender String
            parameter name and the parameter name received transmission not at the same time, the use of @RequestParam binding;

  • @PathVariable
    RESTful风格
      http://localhost:7777/hello/rest/10/a
@RequestMapping("/rest/{id}/{name}")
public String rest(@PathVariable("id") int id,@PathVariable("name") String name){
  System.out.println("rest");
  System.out.println(id);
  System.out.println(name);
  return "index";
}

    Use @PathVariable ( "id") int id, @PathVariable ( "name") String name binding parameters

 

  • @CookieValue
    Cookie map
@RequestMapping("/cookie")
public String cookie(@CookieValue("JSESSIONID") String sessionId){
  System.out.println(sessionId);
  return "index";
}

 

  • Bind parameters using POJO
package com.sunjian.entity;

/**
 * @author sunjian
 * @date 2020/3/17 14:25
 */
public class User {
    private Integer id;
    private String name;
    private Integer age;
    private Address address;

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", address=" + address +
                '}';
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    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;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }
}
package com.sunjian.entity;

/**
 * @author sunjian
 * @date 2020/3/17 14:26
 */
public class Address {
    private Integer id;
    private String name;

    @Override
    public String toString() {
        return "Address{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
package com.sunjian.controller;

import com.sunjian.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @author sunjian
 * @date 2020/3/17 14:28
 */
@Controller
@RequestMapping(value = "/user")
public class UserHandler {
    @RequestMapping(value = "/add")
    public String add(User user){
        System.out.println(user);
        return "index";
    }
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
<form action="/user/add" method="post">
    ID:<input type="text" name="id"/><br/>
    姓名:<input type="text" name="name"/><br/>
    年龄:<input type="text" name="age"/><br/>
    地址ID:<input type="text" name="address.id"/><br/>= "text"of the typethe INPUT<
    Address Name: name="address.name"/><br/>
    <input type="submit" value="提交"/>
</form>
</body>
</html>

Visit http: // localhost: 7777 / add.jsp, enter personnel information, click the submit button, the print results

User{id=123, name='张三', age=31, address=Address{id=1234, name='12345'}}





 

OK. 

Guess you like

Origin www.cnblogs.com/zuichao123/p/12510616.html