1、可以直接在RequestMapping中value元素中使用{key}描述屬性鍵
2、也可以在{key}中使用正則限定key的取值范圍,從而限定url的變化范圍
package com.jt; 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.ResponseBody; @Controller @RequestMapping(value="/FirstControl") public class HelloControl { @RequestMapping(value="/var/{name}/{id}") @ResponseBody public String viewVar(@PathVariable String name,@PathVariable String id){ System.out.println("name "+name); System.out.println("id "+id); return ""+name+id; } @RequestMapping(value="/varregs/{name:[a-z]+}/{id:[0-9]+}") @ResponseBody public String viewVarReg(@PathVariable String name ,@PathVariable String id){ return ""+name+id; } }
1、未使用正則限定的情況下效果

2、使用正則限定的情況下效果

3、使用正則限定的情況下效果(第一個參數不符合正則匹配)

4、使用正則限定的情況下效果(第二個參數不符合正則匹配)

