SpringBoot多種場景傳參模式
web技術(shù)
我們知道常見的web技術(shù)也就是網(wǎng)站開發(fā),分為靜態(tài)網(wǎng)站,和動態(tài)網(wǎng)站,動態(tài)網(wǎng)站技術(shù)常見的有三種,分別是 jsp java web,asp c# web,php web但是它們對應請求request,響應response 都是一樣的我們用java web開發(fā)動態(tài)網(wǎng)站用的mvc框架就是,springmvc,當然我們現(xiàn)在用的是springboot 它只是對spirng全家桶的一個整合框架,他本質(zhì)不是一個新的框架,內(nèi)部還是spirng+springmvc
多種傳參方式
傳統(tǒng)參數(shù)傳遞
我們知道controller方法中會幫我注入HttpServletRequest對象,我們可以通過
request.getParameter("參數(shù)名")來直接獲取參數(shù),
@RequestMapping("/test01")
public ModelAndView test01(HttpServletRequest request){
String username = request.getParameter("username");
String password = request.getParameter("password");
System.out.println(username);
System.out.println(password);
return null;
}
簡單類型參數(shù)映射
- 如果請求參數(shù)和Controller方法的形參同名,可以直接接收
- 如果請求參數(shù)和Controller方法的形參不同名,可以使用@RequestParam注解貼在形參前,設(shè)置對應的參數(shù)名稱
注意這里只能是基本數(shù)據(jù)類型比如string,int,long,boolean等類型
@RequestMapping("/test02_1")
public ModelAndView test02_1(String username,String password){
System.out.println(username);
System.out.println(password);
return null;
}
@RequestMapping("/test02_2")
public ModelAndView test02_2(@RequestParam("username") String name,@RequestParam(value = "password",defaultValue = "1234987") String pwd){
//使用了@RequestParam的參數(shù)不能傳空值
// required:表示是否必須要傳值
// defaultValue:當沒有該請求參數(shù)時,SpringMVC給請求參數(shù)的默認值
System.out.println(name);
System.out.println(pwd);
return null;
}
復雜對象映射
當然在實際項目中,我們會有很多個參數(shù),一般超過兩個參數(shù)我們就要封裝成對象,通過對象傳參數(shù),不然這么一個一個寫會麻煩,代碼冗余,不美觀,不能復用
此時能夠自動把參數(shù)封裝到形參的對象屬性上:
- 請求參數(shù)必須和對象的屬性同名
- 此時對象會直接放入request作用域中,名稱為類型首字母小寫
- @ModelAttribute設(shè)置請求參數(shù)綁定到對象中并傳到視圖頁面的key值
@RequestMapping("/test03")
public ModelAndView test03(@ModelAttribute("stu") Student student){
/*可以使用對象作為方法的形參,同時接受接受前臺的多個請求參數(shù)
SpringMVC會基于同名匹配,將請求參數(shù)的值注入對應的對象中的屬性中
@ModelAttribute起名字key值*/
System.out.println(student);
ModelAndView mv = new ModelAndView();
mv.setViewName("test2");
return mv;
}
如果需要body里面json傳參數(shù)需要在形參前面加上@RequestBody 會自動完成映射
@PostMapping("/register")
public Result bodyParams(@RequestBody Users users) {
return ResultResponse.success(users);
}
數(shù)組和集合類型參數(shù)
當前臺頁面?zhèn)鱽淼膮?shù)是參數(shù)名相同,參數(shù)值不同的多個參數(shù)時,可以直接封裝到方法的數(shù)組類型的形參中
比如批量刪除時傳來的參數(shù)
/*對于參數(shù)名相同的多個請求參數(shù),可以直接使用數(shù)組作為方法的形參接收
可以使用對象中的集合屬性接收*/
@DeleteMapping("/del")
public Result listParams(String[] ids) {
return ResultResponse.success(ids);
}

Restful風格傳參
Restful是一種軟件架構(gòu)風格,嚴格上說是一種編碼風格,其充分利用 HTTP 協(xié)議本身語義從而提供了一組設(shè)計原則和約束條件。
主要用于客戶端和服務器交互類的軟件,該風格設(shè)計的軟件可以更簡潔,更有層次,更易于實現(xiàn)緩存等機制。 在后臺,RequestMapping標簽后,可以用{參數(shù)名}方式傳參,同時需要在形參前加注解@PathVarible
@RequestMapping("/delete/{id}")
public ModelAndView test4(@PathVariable("id")Long id){
System.out.println("delete");
System.out.println(id);
return null;
}
到此這篇關(guān)于SpringBoot多種場景傳參模式的文章就介紹到這了,更多相關(guān)SpringBoot 傳參模式內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java實戰(zhàn)項目 醫(yī)院預約掛號系統(tǒng)
本文是一個Java語言編寫的實戰(zhàn)項目,是一個醫(yī)院預約掛號系統(tǒng),主要用到了jdbc+jsp+mysql+ajax等技術(shù),技術(shù)含量比較高,感興趣的童鞋跟著小編往下看吧2021-09-09
Java基于rest assured實現(xiàn)接口測試過程解析
這篇文章主要介紹了Java基于rest assured實現(xiàn)接口測試過程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-03-03
java.lang.NullPointerException出現(xiàn)的幾種原因及解決方案
這篇文章主要介紹了java.lang.NullPointerException出現(xiàn)的幾種原因及解決方案,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-05-05

