MVC get,post接收引數的幾種方式
註釋上都寫得很清楚哦
/**
* Description: MVC get,post接收引數的幾種方式
* 配合postman模擬請求來測試
*/
@RestController
@RequestMapping("/mvc")
public class MvcPostAndGet {
private static final Logger LOGGER = LoggerFactory.getLogger(MvcPostAndGet.class);
/**
* localhost:2000/mvc/get1?name=小明
* 直接在方法體寫 ?後的引數
*/
@GetMapping("/get1")
public void getParamByGET1(String name) {
LOGGER.info("get1接收到的引數:{}", name);
}
/**
* 使用HttpServletRequest獲取引數
* localhost:2000/mvc/get2?name=小明
* @param request
*/
@GetMapping("/get2")
public void getParamByGET2(HttpServletRequest request) {
String name = request.getParameter("name");
LOGGER.info("get2接收到的引數:{}", name);
}
/**
* localhost:2000/mvc/get3?name=小明
* 使用@requestParam註解獲取引數
*/
@GetMapping("/get3")
public void getParamByGET3(@RequestParam String name) {
LOGGER.info("get3接收到的引數:{}", name);
}
/**
* localhost:2000/mvc/get4/ABC
* 注意用該註解獲取get請求引數時,有坑,引數是中文或者帶點時,解析不到,查詢資料需要設定tomcat配置
* 使用@PathVariable註解獲取
*/
@RequestMapping("/get4/{name}")
public void getParamByGET4(@PathVariable(name = "name", required = true) String name) {
LOGGER.info("get4接收到的引數:{}", name);
}
/**
* 下面post請求的請求體:
* {
"name":"小明",
"idType":"0",
"idno":"123"
}
*/
/**
* 使用@RequestBody獲取,Content-Type是application/json
* @param userKey 對應引數中的每個欄位
*/
@PostMapping("/post1")
public void getParamByPOST1(@RequestBody UserKey userKey){
LOGGER.info("post1接收到的引數:{}",userKey);
}
/**
* 使用Map來獲取
* map中存放的鍵值對就對應於json中的鍵值對 content-type:application/json
*/
@PostMapping("/post2")
public void getParamByPOST2(@RequestBody Map<String,String> map){
String name = map.get("name");
String idNo = map.get("idNo");
String idType = map.get("idType");
LOGGER.info("post2獲取到的引數:{}.{},{}",name,idNo,idType);
}
/**
* 使用HttpServletRequest來獲取,這裡必須把content-type改為x-www-form-urlencoded方式才可以
*/
@PostMapping("/post3")
public void getParamByPOST3(HttpServletRequest request){
String name = request.getParameter("name");
String idType = request.getParameter("idType");
String idNo = request.getParameter("idNo");
LOGGER.info("post2獲取到的引數:{}.{},{}",name,idNo,idType);
}
}
相關推薦
MVC get,post接收引數的幾種方式
註釋上都寫得很清楚哦/** * Description: MVC get,post接收引數的幾種方式 * 配合postman模擬請求來測試 */@RestController@RequestMapping("/mvc")public class MvcPostAndGet { private stat
SpringMVC後臺接收引數的幾種方式
一、@RequestParam 註解 在處理方法入參處使用 @RequestParam 可以把請求引數傳遞給請求方法的形參。 @RequestParam 註解的作用 value=”name” 表示引數名稱。 required=true 表示如
前端ajax非同步傳值以及後端接收引數的幾種方式
原文參考 非同步傳值 前臺往後臺傳值呢,有很多種方式,大家聽我細細道來。 第一種呢,也是最簡單的一種,通過get提交方式,將引數在連結中以問號的形式進行傳遞。 // 前臺傳值方法 // 觸發該方法呼叫ajax function testAjax(yourData) {
spring mvc中接收表單提交的資料的幾種方式
spring mvc封裝資料的物件有session、request、ModelAndView、ModelMap、Model、Map Map map,Model model,ModelMap mmap,ModelAndView mav,HttpServletRequest r
springmvc Controller接收前端引數的幾種方式總結
(1) 普通方式-請求引數名和Controller方法的引數一致 1 @Controller 2 @RequestMapping("/param") 3 public class TestParamController { 4 private static fin
spring-boot-route(一)Controller接收引數的幾種方式
Controller接收引數的常用方式總體可以分為三類。第一類是Get請求通過拼接url進行傳遞,第二類是Post請求通過請求體進行傳遞,第三類是通過請求頭部進行引數傳遞。 ### 1 @PathVariable接收引數 請求方式:localhost:7001/param/123 請求示例: ![](
ASP.NET MVC下自定義錯誤頁和展示錯誤頁的幾種方式
提供服務 one url attribute 運行 16px execute 釋放 namespace 在網站運行中,錯誤是不可避免的,錯誤頁的產生也是不可缺少的。 這幾天看了博友的很多文章,自己想總結下我從中學到的和實際中配置的。 首先,需要知道產生錯誤頁的來源,一種
springMVC接收請求參數的幾種方式
參考資料 let http lmap int 會有 delattr bmi elm 1. 用註解@RequestParam綁定請求參數 用註解@RequestParam綁定請求參數a到變量a,當請求參數a不存在時會有異常發生,可以通過設置屬性required=false解
原生PHP接收$_POST的幾種方式
> HTTP 常見 Content-Type > application/x-www-form-urlencoded> multipart/form-data> application/json > $_POST 預設只能接收到 Conte
後臺引數驗證的幾種方式
前臺和後臺驗證(MVC、Struts2)的必要性經驗總結: 1.後端驗證是必需的,只有後端驗證才能保證表單資料輸入的合法性,前端驗證的主要目的是為了方便使用者,增強使用者體驗。2.雖然不是必需的,但目前也算是一種發展趨勢,特別是面向一般使用者的網站,沒有加前端驗證可能會加大使用者註冊跑路率。3.前端驗證方式
前端ajax異步傳值以及後端接收參數的幾種方式
ping 回調 button 進行 到你 del 後臺 log 註意 原文參考 異步傳值 前臺往後臺傳值呢,有很多種方式,大家聽我細細道來。 第一種呢,也是最簡單的一種,通過get提交方式,將參數在鏈接中以問號的形式進行傳遞。 // 前臺傳值方法 // 觸發該方法調
spring mvc 實現遠端服務呼叫的幾種方式
org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter 實現遠端服務呼叫 (1)httpinvoker方式 伺服器客戶端都是spring時推薦這種方式 服務端 必須要實現 bean實體類
微信小程式頁面之間傳遞引數的幾種方式
小程式開發過程中,不可避免要遇到不同頁面之間資料通訊的問題,如判斷是否登入等,現做出以下分類總結 一、使用全域性變數實現資料傳遞 使用場景: 購物車介面需要根據是否登入來區別顯示,當沒有登入時提醒去登入,登入後之間顯示自己購物車裡的商品列表
Spring MVC中返回JSON資料的幾種方式
我們都知道Spring MVC 的Controller方法中預設可以返回ModeAndView 和String 型別,返回的這兩種型別資料是被DispatcherServlet拿來給到檢視解析器進行繼續處理返回頁面的,而不是直接返回給客戶端的。有時候我們需要發請求後讓服務端直接返回一些資料,不再經過Dispa
Spring MVC中返回JSON數據的幾種方式
return res set 轉換 public servlet 兩種 dispatch ont 我們都知道Spring MVC 的Controller方法中默認可以返回ModeAndView 和String 類型,返回的這兩種類型數據是被DispatcherServlet
thinkphp中URL傳引數的幾種方式
在thinkphp中,url傳參合asp.net中原理類似,下面就單個引數和多個引數傳遞方式進行一個簡單講解1.傳單個引數 單個引數這種比較簡單,例如 想像edit操作裡面傳遞一個id值,如下寫法__URL__/edit/id/11http://localhost/index.php/user/edit/id
實現引數傳遞的幾種方式
由於web系統採用http協議在瀏覽器和伺服器之間傳輸資料,而http協議是一種無狀態的協議,如何在不同頁面之間、或頁面向後端傳遞資料,可以有一下幾種方式 方式一:表單方式傳遞 表單傳遞引數是一種最簡單,也是最基本的引數傳遞方式。注意:表單元素隱藏按鈕的使
MVC教程四:Controller向View傳值的幾種方式
一、通過ViewData傳值 MVC從開始版本就一直支援使用ViewData將Controller裡面的資料傳遞到View。ViewData定義如下: 從上面的截圖中可以看出,ViewData裡面存的是字典型別的資料,在檢視ViewDataDictionary的定義: 注意:ViewDataDi
jmeter的JDBC Request中進行引數設定的幾種方式
JDBC Request: 這個sampler可以向資料庫傳送一個jdbc請求(sql語句),它經常需要和JDBC Connection Configuration 配置元件一起配合使用。 目錄: 一、準備工作 二、配置JDBC Connection Co
flask配置引數的幾種方式,及用法
第一種 使用配置檔案 app=Flask(__name__) app.config.from_pyfile("config.cfg") #在同一級別下建立一個檔案 第二種 物件的方法 class Config(object): DEBUG=True IT