`

@RequestMapping中请求URL绑定的占位符

阅读更多

1、@RequestMapping中请求URL绑定的占位符

(1)带占位符的URL是Spring3.0新增的功能,该功能在SpringMVC向REST挺进发展过程中具有里程碑的意义。
(2)通过 @PathVariable可以将 URL中占位符参数绑定到控制器处理方法的入参中:URL中的{xxx}占位符可以通过@PathVariable("xxx")绑定到操作方法的入参中。

 

2、控制器TestRequestMappingController.java

package com.springmvc.web.controller;

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("/springmvc")
public class TestRequestMappingController {

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

 

3、访问代码

<a href="<%=path%>/springmvc/testPathVariable/10/aaa">RequestMapping中请求URL的占位符</a>

 

 

分享到:
评论
2 楼 Noenemy 2019-03-15  
@RequestMapping请求路径URL中的动态参数是否是必须的吗?什么时候该包含动态参数,什么时候不包含,主要是看方法是否有传参数吗,有的方法有参数但是没有对应的动态参数?
1 楼 Noenemy 2019-03-15  
@RequestMapping请求路径URL中的动态参数是否是必须的,什么时候该包含动态参数,什么时候不包含,主要是看方法是否要求传参数吗,有的方法有参数但是没有对应的动态参数?

相关推荐

Global site tag (gtag.js) - Google Analytics