SpringBoot復(fù)雜參數(shù)應(yīng)用詳細(xì)講解
復(fù)雜參數(shù):
- Map<String, Object> map
- Model model
- HttpServletRequest request
- HttpServletResponse response
以上復(fù)雜參數(shù)所攜帶的數(shù)據(jù)均可被放在 request 請求域中,其中 Map 與 Model 類型處理方法一致。(本文只介紹使用)
使用方法:
1. controller 類完整代碼:
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;
@Controller
public class RequestController {
@GetMapping("/params")
public String testParam(Map<String, Object> map,
Model model,
HttpServletRequest request,
HttpServletResponse response){
map.put("map", "helloMap");
model.addAttribute("model", "helloModel");
request.setAttribute("message", "helloMessage");
Cookie cookie = new Cookie("c1", "v1");
cookie.setDomain("localhost");
response.addCookie(cookie);
return "forward:/success"; // 轉(zhuǎn)發(fā)到 /SUCCESS請求
}
@ResponseBody
@GetMapping("/success")
public Map success(HttpServletRequest request){
Map<String, Object> map = new HashMap<>();
Object hello = request.getAttribute("map");
Object model = request.getAttribute("model");
Object message = request.getAttribute("message");
map.put("hello", hello);
map.put("medol", model);
map.put("message", message);
return map;
}
}2. 具體解釋:
- map、model 里面的數(shù)據(jù)會被放在request的請求域, 通過request.getAttribute(“數(shù)據(jù)名”) 取得。
- HttpServletRequest 的數(shù)據(jù)也會被放在request的請求域, 通過request.getAttribute(“請求名”) 取得。
注意:使用return "forward:/success"轉(zhuǎn)發(fā)機(jī)制,Controller的注釋為 @Controller
3. 執(zhí)行結(jié)果:
通過request取得 Map,Medol,HttpServletRequest 的值如下圖所示:

設(shè)置cookies成功:

尾注:我是看尚硅谷老師的課學(xué)習(xí)的SpringBoot,30分鐘的課25分鐘debug看源碼(新手不友好),所以開始時真的很困難,可是只要跑起來就有風(fēng)不是嘛,哼,死磕到底!
到此這篇關(guān)于SpringBoot復(fù)雜參數(shù)應(yīng)用詳細(xì)講解的文章就介紹到這了,更多相關(guān)SpringBoot復(fù)雜參數(shù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
spring boot使用sharding jdbc的配置方式
這篇文章主要介紹了spring boot使用sharding jdbc的配置方式,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-12-12
Spring Boot 中使用 JSON Schema 校驗復(fù)雜JSO
在數(shù)據(jù)交換領(lǐng)域,JSON Schema 以其強(qiáng)大的標(biāo)準(zhǔn)化能力,為定義和規(guī)范 JSON 數(shù)據(jù)的結(jié)構(gòu)與規(guī)則提供了有力支持,下面給大家介紹Spring Boot 中使用 JSON Schema 校驗復(fù)雜JSON數(shù)據(jù)的過程,感興趣的朋友跟隨小編一起看看吧2024-08-08
springmvc @ResponseStatus和ResponseEntity的使用
這篇文章主要介紹了springmvc @ResponseStatus和ResponseEntity的使用方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-07-07
Spring?boot詳解fastjson過濾字段為null值如何解決
這篇文章主要介紹了解決Spring?boot中fastjson過濾字段為null值的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07
java通過客戶端訪問服務(wù)器webservice的方法
這篇文章主要介紹了java通過客戶端訪問服務(wù)器webservice的方法,涉及java創(chuàng)建與調(diào)用webservice的相關(guān)技巧,需要的朋友可以參考下2016-08-08

