關於requestMapping 進行url映射實現小小知識點 以及如何獲取請求的url中的參數


requstMapping 用來處理url映射  可以作用在controller類上  也可以作用在方法上

經常使用的方式  通過接收一種映射關系

   @RequestMapping("/deleteMainMultipleMessages")
    public ModelAndView deleteMainMultipleMessages(String id[]){
        for (int i = 0; i < id.length; i++) {
            service.delete(id[i]);
        }
        return new ModelAndView("redirect:/user/home");
    }

其實requstMapping可以接收多個請求方式   通過localhost:8080/hello  與localhost:8080/hi 都可以訪問同一個請求方法

  @RequestMapping(value = {"/hello","/hi"},method = RequestMethod.GET)
    public String say(){

       return gril.getName();
    }

 

在requesetMapping 可以指定method 請求的類型比如get  post 等

同時也可以忽略讓服務器自己判斷  但是不希望這樣做  通常我們希望指定請求方式的做法來做  這樣是安全的  post get 等都是對應不同業務情況的

以前都沒注意過今天記錄一下。。。。。

 

@PathVariable 獲取url中的數據

 請求方式:http://localhost:9001/hello/34

 @RequestMapping(value = {"/hello/{id}"},method = RequestMethod.GET)
    public String say(@PathVariable(value = "id")String id){

       return id;
    }

也可以寫在前面  http://localhost:9001/34/hello

 @RequestMapping(value = {"/{id}/hello"},method = RequestMethod.GET)
    public String say(@PathVariable(value = "id")String id){

       return id;
    }

 

@RequstParam 獲取請求參數的值

@getMapping 組合注解

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM