@PathVariable注解使用

本文详细介绍了SpringMVC框架中@PathVariable注解的使用方法,包括其语法、如何在控制器方法中接收URL中的占位符参数,并通过示例展示了具体的实现过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

@PathVariable是spring3.0的一个新功能:接收请求路径中占位符的值

语法:

@PathVariable("xxx")
通过 @PathVariable 可以将URL中占位符参数{xxx}绑定到处理器类的方法形参中@PathVariable(“xxx“) 

@RequestMapping(value=”user/{id}/{name}”)
请求路径:http://localhost:8080/hello/show5/1/james

测试环境:

环境:jdk1.8 Tomcat8.5  idea2018  manven父工程子模块
 
步骤:
1、创建web工程、引入依赖
2、配置SpringMvc入口文件 --DispatcherServlet--为总调度、web.xml里配置
3、创建Springmvc.xml文件--理解为:适配器(这里不需要自已指定适配、springmvc会自动指定)--视图解析器
4、创建 业务处理器 Controller类
5、测试

工程结构:

 

步骤1、2、3、参考:SpringMvc入门案例:https://blog.youkuaiyun.com/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;

/**
 * @ Author     :ShaoWei Sun.
 * @ Date       :Created in 20:58 2018/11/16
 */
@Controller
@RequestMapping("hello")
public class HelloController2 {
    /**
     *3、占位符映射
     * 语法:@RequestMapping(value=”user/{userId}/{userName}”)
     * 请求路径:http://localhost:8080/hello/show5/1/james
     * @param ids
     * @param names
     * @return
     */
    @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");
        return mv;
    }
}

测试

 

                                      公众号  关注一波  不定期分享视频资料

                                                        

 

### Spring 中 @PathVariable 注解使用方法 @PathVariable 注解在 Spring MVC 和 Spring Boot 中用于从 URI 模板中提取路径变量,并将其绑定到方法参数上。这种注解通常用于 RESTful 风格的 URL 路径中,其中路径的一部分是动态的,并且可能需要根据请求的不同而改变[^4]。 以下是一个简单的示例代码,展示如何使用 @PathVariable 注解: ```java import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; @RestController public class ProductController { @GetMapping("/products/{category}/{id}") public String getProductByCategoryAndId(@PathVariable String category, @PathVariable Long id) { return "Product in category " + category + " with ID " + id; } } ``` 在这个例子中,`/products/{category}/{id}` 是一个 URI 模板,其中 `{category}` 和 `{id}` 是占位符。当客户端发送一个类似于 `/products/electronics/123` 的请求时,Spring 会自动将 `electronics` 绑定到 `category` 参数,将 `123` 绑定到 `id` 参数[^2]。 如果需要为路径变量指定默认值,可以结合逻辑判断实现。例如: ```java import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/demo") public class UserController { @GetMapping("/{userId}") public String getUserById(@PathVariable("userId") String id, @PathVariable(name = "userId", required = false) Integer optionalId) { if (optionalId != null) { return "User with ID: " + id + " and Optional ID: " + optionalId; } else { return "User with ID: " + id; } } } ``` 在此示例中,`optionalId` 参数被标记为非必需(`required = false`)。如果路径中未提供该参数,则其值为 `null`,可以通过条件语句进行处理[^5]。 ### 注意事项 - 占位符在方法参数中可以通过 `{variable}` 的形式定义,并通过 @PathVariable 注解映射到具体参数上[^1]。 - 在同一个 URL 中可以使用多个占位符,并将它们映射到不同的方法参数上。 - 路径变量没有直接的默认值支持,但可以通过逻辑判断来模拟默认行为[^5]。
评论 66
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值