標籤:spring requestmapping getparameter
@RequestMapping請求路徑映射,如果標註在某個controller的類層級上,則表明訪問此類路徑下的方法都要加上其配置的路徑;最常用是標註在方法上,表明哪個具體的方法來接受處理某次請求。
以下兩種方式都可以從url中傳參數,但是第二種方式的適用性更高一些,當參數中包含中文的時候,如果用第一種方式傳參數,經常會出現參數還沒到controller就已經經過編碼了(例如:經過utf-8編碼後,原本要傳的參數就會以%+ab...cd這樣的方式出現),然後controller接受到這樣的請求後,根本無法解析該請求應該走那個業務方法。然後就會出現常見的404問題。。。
package com.test.jeofey.web;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;@Controller@RequestMapping("/path")public class TestController {// 第一種傳參數的方式 訪問地址例如:http:網域名稱/path/method1/keyWord.html@RequestMapping("method1/{keyWord}")public String getZhiShiDetailData(@PathVariable("keyWord") String keyWord,HttpServletRequest request, HttpServletResponse response){System.out.println(keyWord);return "v1/detail";}// 第二種傳參數的方式 訪問地址例如:http:網域名稱/path/method2.html?key=keyWord@RequestMapping("method2")public String getCommonData(HttpServletRequest request, HttpServletResponse response){String keyWord= request.getParameter("key");System.out.println(keyWord);return "v1/common";}}
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。
Spring註解@RequestMapping請求路徑映射問題