- 带占位符的URL新增的功能,该功能在SpringMVC想REST目标挺进发展过程中具有里程碑的意义*
- 通过@PatchVariable可以将URL中的占位符参数绑定到控制器处理方法的入参中:URL 中的{xxx}占位符可以通过@PathVariable("xxx")绑定到操作方法的入参中。
package com.gu.controller;
import org.omg.CORBA.Request;
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;
/**
* Created by gurongkang on 17/5/8.
*/
@Controller
@RequestMapping(value = "/controller")
public class HelloController {
/**
* @PathVariable可以用来映射URL的占位符到目标方法的参数中
*/
@RequestMapping("/testPathVariable/{id}")
public String testPathVariable(@PathVariable("id") Integer id) {
System.out.printf("testPathVariable------>" + id);
return "Hello";
}
}
url测试
<a href="controller/testPathVariable/10">testPathVariable</a>
id 为10
网友评论