Spring MVC常用客戶端參數(shù)接收方式詳解
在MVC結構中,控制器組件主要的功能就是接收請求、處理請求、生成響應,接收客戶端傳來的請求參數(shù)的往往是控制器要做的第一件事。
Book實體類Book.java
public class Book { private Integer bookId; private String author; //生成Get、Set方法,此處省略 }
一、直接用參數(shù)名匹配請求參數(shù)
客戶端界面(表單):
<form action="/queryString" method="post"> <input type="text" name="bookId"> <input type="text" name="author"> <input type="submit" value="提交"> </form>
controller層:
@Controller public class ParamPassDemo { @RequestMapping(value="/queryString") public String test1(Integer bookId, String author) { System.out.println("bookId="+bookId+", author="+author); //此處返回的地址為(/WEB-INF/jsp/index.jsp) return "index"; } }
注意:這里@RequestMapping中只有value屬性,value可以省略不寫。
客戶端輸入:123,Rose
控制臺輸出:bookId=123, author=Rose
二、通過@RequestParam注解來指定請求參數(shù)的name
客戶端界面(表單):
<form action="/queryStringWithSpecName" method="post"> <input type="text" name="bookId" value="321"> <input type="text" name="author" value="Jack"> <input type="submit" value="提交"> </form>
如果表單中的字段與方法中的參數(shù)名一致,可以不需要@RequestParam,Spring會自動處理。
controller層:
@Controller public class ParamPassDemo { @RequestMapping("/queryStringWithSpecName") public String test2((value="bookId",required=false) Integer id, @RequestParam("author") String name) { System.out.println("bookId="+id+", author="+name); return "index"; } }
注意:這里@RequestMapping中有兩個屬性,value不能省略。
@RequestParam將請求地址中的參數(shù)傳遞給目標方法,在處理方法入?yún)⑻幨褂每梢园颜埱髤?shù)傳遞給請求方法。
當使用@RequestParam注解時,設置客戶端傳遞的請求參數(shù)name="bookId"和@RequestParam的value值value="bookId"相匹配后,參數(shù)名int id可以和請求參數(shù)不匹配。
客戶端輸入:321, Jack
控制臺輸出:bookId=321, author=Jack
客戶端界面(ajax):
<button onclick="clickMe()">點我</button> <script> function clickMe() { $.ajax({ type : 'POST', url : "/queryStringWithSpecName", data : { "bookId" : 1, "author" : "Jack" }, }); } </script>
controller層:(不變)
客戶端: data:{"author" : "Jack"}
控制臺輸出: bookId=null, author=Jack(如果bookId為int類型,控制臺會拋出異常)
客戶端: data:{"bookId" : 1}
控制臺輸出: org.springframework.web.bind.MissingServletRequestParameterException: Required String parameter 'author' is not present
通過required設置可選參數(shù),required為false時表示可以不帶參數(shù),為true時表示必須帶參數(shù)(默認值為true)。
當可選參數(shù)不存在時,Spring默認將其賦值為空(null),但由于bookId已定義為基本類型int,所以賦值會失敗。解決方法:采用int包裝類Integer。
三、使用領域對象來接收參數(shù)
客戶端界面(表單):
<form action="/queryStringWithDomainObj" method="post"> <input type="text" name="bookId"> <input type="text" name="author"> <input type="submit" value="提交"> </form>
controller層:
@Controller public class ParamPassDemo { @RequestMapping("/queryStringWithDomainObj") public String test3(Book book) { System.out.println("bookId="+book.getBookId()+", author="+book.getAuthor()); return "index"; } }
客戶端輸入:111, Bob
控制臺輸出:bookId=111, author=Bob
四、URL動態(tài)參數(shù)傳遞(路徑參數(shù))
客戶端界面(超鏈接):
<a href="/book/1" rel="external nofollow" >testPathVariable</a>
controller層:
@Controller public class ParamPassDemo { //@PathVariable可以用來映射URL中的占位符到目標方法的參數(shù)中 @RequestMapping("/book/{bookId}") public String test4(@PathVariable("bookId") Integer bookId) { System.out.println("bookId:" + bookId); return "index"; } }
控制臺輸出:bookId:1
@PathVariable 映射 URL 綁定的占位符
通過 @PathVariable 可以將 URL 中占位符參數(shù)綁定到控制器處理方法的入?yún)⒅校篣RL 中的 {xxx} 占位符可以通過@PathVariable(“xxx“) 綁定到操作方法的入?yún)⒅小?/p>
五、使用HttpServletRequest獲取請求參數(shù)
客戶端界面(表單):
<form action="/queryBook" method="post"> <input type="text" name="bookId"> <input type="text" name="author"> <input type="submit" value="提交"> </form>
controller層:
@Controller public class ParamPassDemo { @RequestMapping("/queryBook") public String test5(HttpServletRequest request) { System.out.println("bookId:" + request.getParameter("bookId")); //此處index.jsp界面在WEB-INF下 return "redirect:/index.jsp"; } }
客戶端輸入:123
控制臺輸出:用戶id:123
六、跳轉到另一個controller方法
客戶端界面(url地址欄):http://localhost:8080/test6?bookId=321
controller層:
@Controller public class ParamPassDemo { @RequestMapping("/test6") public String test6(String bookId){ System.out.println("bookId="+bookId); //使用服務端跳轉的方式轉向到另一個controller //return "forward:queryBook?bookId="+bookId; return "redirect:queryUser?bookId="+bookId; } }
控制臺輸出:bookId=321 bookId:321
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
解決springboot報錯找不到自動注入的service問題
這篇文章主要介紹了解決springboot報錯找不到自動注入的service問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08MyBatis?Plus實現(xiàn)中文排序的兩種有效方法
在MyBatis?Plus項目開發(fā)中,針對中文數(shù)據(jù)的排序需求是一個常見的挑戰(zhàn),尤其是在需要按照拼音或特定語言邏輯排序時,本文整合了兩種有效的方法,旨在幫助開發(fā)者克服MyBatis?Plus在處理中文排序時遇到的障礙,需要的朋友可以參考下2024-08-08Java基于Guava Retrying實現(xiàn)重試功能
這篇文章主要介紹了Java基于Guava Retrying實現(xiàn)重試功能,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-07-07SpringMVC視圖轉發(fā)重定向區(qū)別及控制器詳解
這篇文章主要為大家介紹了SpringMVC視圖轉發(fā)重定向區(qū)別及控制器示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-05-05mybatis批量update時報錯multi-statement not allow的問題
這篇文章主要介紹了mybatis批量update時報錯multi-statement not allow的問題及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-10-10