RequestMapping注解里面包含的参数如图
value: 控制器名字也是url的映射
params:url中的参数,可以是多个
method: http请求的动作. get post put delete...
headers : 请求头参数
@PathVariable :映射 URL 绑定占位 ( 映射URL路径里面的变量)
@Controller //映射 控制器类 @RequestMapping("/hellotest") public class Hello2 { public final static String DISPATCHER ="show"; @RequestMapping("/test") public String testUrl(){ System.out.println("testUrl=http://localhost:8080/springMVC/hellotest/test"); return DISPATCHER; } /** * get动作请求的测试 * @return */ @RequestMapping(method=RequestMethod.GET,value="/testMethodGet") public String testMethodGet(){ System.out.println("get.............."); return DISPATCHER; } /** * post动作请求的测试 * @return */ @RequestMapping(method=RequestMethod.POST,value="/testMethodPost") public String testMethodPost(){ System.out.println("post.............."); return DISPATCHER; } /** * 测试url中带参数(未测试请求头信息) * @return */ @RequestMapping(value="/testHeadersAndParams", params={"username","sex!=1","password=123"}, method=RequestMethod.GET) public String testHeadersAndParams(){ //url http://localhost:8080/springMVC/hellotest/testHeadersAndParams?username=10&password=123&sex=2 System.out.println("testHeadersAndParams"); return DISPATCHER; } //PathVariable 映射 URL 绑定占位 ( 映射URL路径里面的变量) @RequestMapping(value="/testPathVariable/{id}",method=RequestMethod.GET) public String testPathVariable(@PathVariable("id") Integer id){ System.out.println("testPathVariable id:"+id); //http://localhost:8080/springMVC/hellotest/testPathVariable/5 return DISPATCHER; } @RequestMapping(value="/testPathVariable2/{name}",method=RequestMethod.GET) public String testPathVariable2(@PathVariable("name") String name){ System.out.println("testPathVariable name :"+name); // http://localhost:8080/springMVC/hellotest/testPathVariable2/str return DISPATCHER; } @RequestMapping() public String test(){ return DISPATCHER; } }
时间: 2024-10-21 02:52:07