1)带占位符的URL是Spring3.0新增的功能,该功能在SpringMVC向REST目标挺进发展过程中具有里程碑的意义。

2)通过@PathVariable可以将URL中占位符参数绑定到控制器处理方法的入参中:URL中的{xxx}占位符可以通过@PathVariable("xxx")绑定到操作方法的入参中。

 在HelloWord.java中添加testPathVariable方法:

package com.dx.springlearn.handlers;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("class_requestmapping")
public class HelloWord {
    private static String SUCCESS = "success";

    @RequestMapping("/testPathVariable/{id}")
    public String testPathVariable(@PathVariable(value = "id", required = true) Integer id) {
        System.out.println("testPathVariable: id: " + id);
        return SUCCESS;
    }
}

index.jsp添加链接:

<a href="class_requestmapping/testPathVariable/1">testPathVariable</a>
    <br>

 

相关文章:

  • 2021-11-02
  • 2021-06-20
  • 2022-12-23
  • 2022-12-23
  • 2021-10-07
  • 2021-08-02
  • 2022-12-23
猜你喜欢
  • 2021-12-11
  • 2022-12-23
  • 2022-12-23
  • 2021-09-24
  • 2021-07-01
  • 2022-12-23
  • 2022-01-06
相关资源
相似解决方案