springMVC中可以将指定URL模式地址肯指定访问Controller的路径进行关联与匹配,如果某一个访问Controller控制层的UTL与该URL模式进行匹配,则调用匹配的Controller控制层
controller中的url路径如下,访问时我们只需匹配格式就能完成访问
@RequestMapping("/hello/{userId}")
@PathVariable 或者 @PathVariable("userId")
@RequestMapping("/hello3/{userId}/age/{ageValue}")
@PathVariable
项目实例如下
test
package controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class URLMatchTest {
// 直接使用配置变量
@RequestMapping("/hello/{userId}")
public String test1(@PathVariable String userId) {
System.out.println("run test1 userId=" + userId);
return "/index.jsp";
}
// 对配置变量进行另起别名为userIdParam
@RequestMapping("/hello2/{userId}")
public String test2(@PathVariable("userId") String userIdParam) {
System.out.println("run test2 userIdParam=" + userIdParam);
return "/index.jsp";
}
@RequestMapping("/hello3/{userId}/age/{ageValue}")
public String test3(@PathVariable String userId, @PathVariable int ageValue) {
System.out.println("run test3 userId=" + userId + " ageValue="
+ ageValue);
return "/index.jsp";
}
}
test2
package controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/hello4/{userId}")
public class URLMatchTest2 {
@RequestMapping("/age/{ageValue}")
public String test100(@PathVariable String userId,
@PathVariable int ageValue) {
System.out.println("run test100 userId=" + userId + " ageValue="
+ ageValue);
return "/index.jsp";
}
}