Java Spring Controller 獲取請(qǐng)求參數(shù)的幾種方法詳解
Java Spring Controller 獲取請(qǐng)求參數(shù)的幾種方法
1、直接把表單的參數(shù)寫在Controller相應(yīng)的方法的形參中,適用于get方式提交,不適用于post方式提交。若"Content-Type"="application/x-www-form-urlencoded",可用post提交
url形式:http://localhost:8080/SSMDemo/demo/addUser1?username=lixiaoxi&password=111111 提交的參數(shù)需要和Controller方法中的入?yún)⒚Q一致。
/**
* 1.直接把表單的參數(shù)寫在Controller相應(yīng)的方法的形參中
* @param username
* @param password
* @return
*/
@RequestMapping("/addUser1")
public String addUser1(String username,String password) {
System.out.println("username is:"+username);
System.out.println("password is:"+password);
return "demo/index";
}
2、通過(guò)HttpServletRequest接收,post方式和get方式都可以。
/**
* 2、通過(guò)HttpServletRequest接收
* @param request
* @return
*/
@RequestMapping("/addUser2")
public String addUser2(HttpServletRequest request) {
String username=request.getParameter("username");
String password=request.getParameter("password");
System.out.println("username is:"+username);
System.out.println("password is:"+password);
return "demo/index";
}
3、通過(guò)一個(gè)bean來(lái)接收,post方式和get方式都可以。
/**
* 3、通過(guò)一個(gè)bean來(lái)接收
* @param user
* @return
*/
@RequestMapping("/addUser3")
public String addUser3(UserModel user) {
System.out.println("username is:"+user.getUsername());
System.out.println("password is:"+user.getPassword());
return "demo/index";
}
4、使用@ModelAttribute注解獲取POST請(qǐng)求的FORM表單數(shù)據(jù)
/**
* 4、使用@ModelAttribute注解獲取POST請(qǐng)求的FORM表單數(shù)據(jù)
* @param user
* @return
*/
@RequestMapping(value="/addUser5",method=RequestMethod.POST)
public String addUser5(@ModelAttribute("user") UserModel user) {
System.out.println("username is:"+user.getUsername());
System.out.println("password is:"+user.getPassword());
return "demo/index";
}
5、用注解@RequestParam綁定請(qǐng)求參數(shù)到方法入?yún)?nbsp;
當(dāng)請(qǐng)求參數(shù)username不存在時(shí)會(huì)有異常發(fā)生,可以通過(guò)設(shè)置屬性required=false解決,例如:
@RequestParam(value="username", required=false)
**** 若"Content-Type"="application/x-www-form-urlencoded",post get都可以
**** 若"Content-Type"="application/application/json",只適用get
/**
* 5、用注解@RequestParam綁定請(qǐng)求參數(shù)到方法入?yún)?
* @param username
* @param password
* @return
*/
@RequestMapping(value="/addUser6",method=RequestMethod.GET)
public String addUser6(@RequestParam("username") String username,@RequestParam("password") String password) {
System.out.println("username is:"+username);
System.out.println("password is:"+password);
return "demo/index";
}
6、用request.getQueryString() 獲取spring MVC get請(qǐng)求的參數(shù),只適用get請(qǐng)求
@RequestMapping(value="/addUser6",method=RequestMethod.GET)
public String addUser6(HttpServletRequest request) {
System.out.println("username is:"+request.getQueryString());
return "demo/index";
}
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
- Java中的HttpServletRequestWrapper用法解析
- Java中的HttpServletRequest接口詳細(xì)解讀
- Java獲取HttpServletRequest的三種方法詳解
- Java service層獲取HttpServletRequest工具類的方法
- java通過(guò)HttpServletRequest獲取post請(qǐng)求中的body內(nèi)容的方法
- java HttpServletRequest和HttpServletResponse詳解
- java接口返回參數(shù)按照請(qǐng)求參數(shù)進(jìn)行排序方式
- Java如何獲取HttpServletRequest請(qǐng)求參數(shù)
相關(guān)文章
spring.factories文件的解析源碼API機(jī)制詳解
通過(guò)本文深入探討Spring?Boot的背景歷史、業(yè)務(wù)場(chǎng)景、功能點(diǎn)以及底層原理,使讀者對(duì)Spring?Boot有了更深入的了解,結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-11-11
SpringMVC+ZTree實(shí)現(xiàn)樹形菜單權(quán)限配置的方法
本篇文章主要介紹了SpringMVC+ZTree實(shí)現(xiàn)樹形菜單權(quán)限配置的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-12-12
java運(yùn)行windows的cmd命令簡(jiǎn)單代碼
這篇文章主要介紹了java運(yùn)行windows的cmd命令簡(jiǎn)單代碼,有需要的朋友可以參考一下2013-12-12
java IO流 之 輸入流 InputString()的使用
這篇文章主要介紹了java IO流 之 輸入流 InputString()的使用,以及讀取數(shù)據(jù)的三種方式詳解,非常不錯(cuò),需要的朋友可以參考下2016-12-12
關(guān)于springboot2.4跨域配置問(wèn)題
這篇文章主要介紹了springboot2.4跨域配置的方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-07-07
SpringBoot-RestTemplate實(shí)現(xiàn)調(diào)用第三方API的方式
RestTemplate?是由?Spring?提供的一個(gè)?HTTP?請(qǐng)求工具,它提供了常見的REST請(qǐng)求方案的模版,例如?GET?請(qǐng)求、POST?請(qǐng)求、PUT?請(qǐng)求、DELETE?請(qǐng)求以及一些通用的請(qǐng)求執(zhí)行方法?exchange?以及?execute,下面看下SpringBoot?RestTemplate調(diào)用第三方API的方式2022-12-12
Springboot編寫CRUD時(shí)訪問(wèn)對(duì)應(yīng)數(shù)據(jù)函數(shù)返回null的問(wèn)題及解決方法
我在學(xué)習(xí)springboot,其中在編寫CRUD時(shí)發(fā)現(xiàn)訪問(wèn)數(shù)據(jù)的函數(shù)執(zhí)行下去返回值是null但是其它部分正常,這篇文章主要介紹了Springboot在編寫CRUD時(shí),訪問(wèn)對(duì)應(yīng)數(shù)據(jù)函數(shù)返回null,需要的朋友可以參考下2024-02-02

