原始方式:/deleteUser?id=1
    rest**方式**:/deleteUser/1

    SpringMVC路径中的占位符常用于RESTful风格中,当请求路径中将某些数据通过路径的方式传输到服务器中,就可以在相应的@RequestMapping注解的value属性中通过占位符{xxx}表示传输的数据,在通过@PathVariable注解,将占位符所表示的数据赋值给控制器方法的形参。
    image.png
    Controller:

    1. package com.way.controller;
    2. import org.springframework.stereotype.Controller;
    3. import org.springframework.web.bind.annotation.PathVariable;
    4. import org.springframework.web.bind.annotation.RequestMapping;
    5. @Controller
    6. public class MyController {
    7. @RequestMapping(value = "/target/{id}/{age}")
    8. public String testAntZhan(@PathVariable("id") Integer id,
    9. @PathVariable("age") Integer age) {
    10. System.out.println("id:" + id + "," + "age:" + age);
    11. return "target";
    12. }
    13. }

    HTML:

    1. <!DOCTYPE html>
    2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>Title</title>
    6. </head>
    7. <body>
    8. <h1>首页</h1>
    9. <a th:href="@{/target/1/15}">使用路径占位符</a><br/>
    10. </body>
    11. </html>

    浏览器输出结果:
    image.png
    idea控制台输出结果:

    id:1,age:15