springMVC(14)------PathVariable注解

来源:互联网 发布:2016淘宝客导购源码 编辑:程序博客网 时间:2024/06/15 09:47

@PathVariable注解可以映射spring mvc中请求URL中的占位符到目标方法的参数中。

例如:

@RequestMapping("/testPathVariable/{username}")public String testPathVariable(@PathVariable("username") String username){System.out.println("username="+username);return "success";} 

以上是一个spring mvc请求方法,其中请求地址中{username}为占位符,

当访问URL为:http://localhost:9000/spring-mvc/testPathVariable/tom

会将占位符部分的tom映射到方法参数中的username参数,

通过System.out.println可以从控制台打印输出看到地址中的

占位符部分映射到请求方法的参数中。


注意:RequestMapping映射地址中的占位符{username},即

大括号中的名字username必须与方法参数列表中@PathVariable("username")

的username名字保持一致,否则无法完成占位符与方法参数之间的映射。

0 0
原创粉丝点击