@PathVariable是spring3.0的一个新功能:接收请求路径中占位符的值
语法:
通过 @PathVariable 可以将URL中占位符参数{xxx}绑定到处理器类的方法形参中@PathVariable(“xxx“) @RequestMapping(value=”user/{id}/{name}”) 请求路径:http://localhost:8080/hello/show5/1/james
测试环境:
环境:jdk1.8 Tomcat8.5 idea2018 manven父工程子模块 2、配置SpringMvc入口文件 --DispatcherServlet--为总调度、web.xml里配置 3、创建Springmvc.xml文件--理解为:适配器(这里不需要自已指定适配、springmvc会自动指定)--视图解析器
工程结构:

步骤1、2、3、参考:SpringMvc入门案例:https://blog.csdn.net/sswqzx/article/details/84171999
业务处理器HelloController.java
package com.day01springmvc.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; import org.springframework.web.servlet.ModelAndView; * @ Date :Created in 20:58 2018/11/16 public class HelloController2 { * 语法:@RequestMapping(value=”user/{userId}/{userName}”) * 请求路径:http://localhost:8080/hello/show5/1/james @RequestMapping("show5/{id}/{name}") public ModelAndView test5(@PathVariable("id") Long ids ,@PathVariable("name") String names){ ModelAndView mv = new ModelAndView(); mv.addObject("msg","占位符映射:id:"+ids+";name:"+names); mv.setViewName("hello2");

测试

|