1. 程式人生 > >關於requestMapping 進行url對映實現小小知識點 以及如何獲取請求的url中的引數

關於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 組合註解