springmvc中如何使用@PathVariable实现获取url中的参数

2023-06-05,,

本篇文章给大家分享的是有关springmvc中如何使用@PathVariable实现获取url中的参数,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

spring mvc中的@PathVariable是用来获得请求url中的动态参数

 @Controller 
public class TestController { 
   @RequestMapping(value="/user/{userId}/roles/{roleId}",method = RequestMethod.GET) 
   public String getLogin(@PathVariable("userId") String userId, 
     @PathVariable("roleId") String roleId){ 
     System.out.println("User Id : " + userId); 
     System.out.println("Role Id : " + roleId); 
     return "hello"; 
   } 
   @RequestMapping(value="/product/{productId}",method = RequestMethod.GET) 
   public String getProduct(@PathVariable("productId") String productId){ 
      System.out.println("Product Id : " + productId); 
      return "hello"; 
   } 
   @RequestMapping(value="/javabeat/{regexp1:[a-z-]+}", 
      method = RequestMethod.GET) 
   public String getRegExp(@PathVariable("regexp1") String regexp1){ 
      System.out.println("URI Part 1 : " + regexp1); 
      return "hello"; 
   } 
} 

以上就是springmvc中如何使用@PathVariable实现获取url中的参数,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注本站行业资讯频道。

《springmvc中如何使用@PathVariable实现获取url中的参数.doc》

下载本文的Word格式文档,以方便收藏与打印。