springmvc中的@PathVariable的用法

  spring mvc中的@PathVariable是用來獲得請求url中的動態參數的,十分方便,複習下: 

Java代碼  收藏代碼
  1. @Controller  
  2. public class TestController {  
  3.      @RequestMapping(value="/user/{userId}/roles/{roleId}",method = RequestMethod.GET)  
  4.      public String getLogin(@PathVariable("userId") String userId,  
  5.          @PathVariable("roleId") String roleId){  
  6.          System.out.println("User Id : " + userId);  
  7.          System.out.println("Role Id : " + roleId);  
  8.          return "hello";  
  9.      }  
  10.      @RequestMapping(value="/product/{productId}",method = RequestMethod.GET)  
  11.      public String getProduct(@PathVariable("productId") String productId){  
  12.            System.out.println("Product Id : " + productId);  
  13.            return "hello";  
  14.      }  
  15.      @RequestMapping(value="/javabeat/{regexp1:[a-z-]+}",  
  16.            method = RequestMethod.GET)  
  17.      public String getRegExp(@PathVariable("regexp1") String regexp1){  
  18.            System.out.println("URI Part 1 : " + regexp1);  
  19.            return "hello";  
  20.      }  
  21. }  
發佈了3 篇原創文章 · 獲贊 4 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章