SpringMVC 接收前端傳遞的參數四種方式
- @RequestParam注解
- @PathVariable注解
- SpringMVC的自動解析參數
- SpringMVC的@RequestBody注解
@RequestParam 獲取注解
1
2
3
|
get/post url => "xx/user?id=1" action => public String User( @RequestParams (name= "id" ) Long id ){ } |
@RequestParam定義的參數 會自動解析為 方法定義的類型。
1
|
@RequestParams (name= "id" ) Long id )(通過postman模擬post請求) |
@PathVariable獲取注解
1
|
get/post url => "xx/user/1" action => public String User( @PathVariable (name= "id" ) Long id){} |
@PathVariable必須通過正則指定對應的類型 只有當url指定為數字,方法參數定義為數字類型才不會報錯。比如:(可以通過其他正則限制url,只有符合條件的url才會映射到對應的action,否則不會找到對應的action)
1
2
|
@RequestMapping ( "/user/{id:\\d}" ) public String User( @PathVariable (name= "id" ) Long id){} |
SpringMVC,可以不設置任何注解即可接收參數
比如
1
2
3
4
5
|
@GetMapping ( "/category" ) public String category( Long id) { System.out.println(id); return "post/category" ; } |
可以通過 /category 訪問 ,也可以通過 /category?id=1 訪問
SpringMVC,也可以自動包裝成對象
url /category?title=測試 或者 /category 都能訪問到目標資源
1
2
3
4
5
|
@GetMapping ( "/category" ) public String category( MPost post ) { System.out.println(post.getTitle()); return "post/category" ; } |
@RequestBody 用來接收數組或者復雜對象
(必須將參數放在requestbody中,放在url并不會被解析,哪怕請求方式是post)
1
2
3
4
5
6
|
url => /category requestbody =>{ "id" : 1 } @PostMapping ( "/category" ) public String category( @RequestBody Post post ) { System.out.println(post.getTitle()); return "post/category" ; } |
若為對象數組,將方法參數改為 @RequestBody List<Post> post 即可
直接輸入 /category并不會找到對應的action
SpringMVC的自動封裝(不傳參也能進入)
-
@RequestParam
(必須傳參,但可以手動設置為false) -
@PathVariable
(符合設定的正則表達式才允許進入,而且不能為空)
對比可知,主要是為了url提供更加嚴格的限制,以防止一些其他url進入該action。
提供復雜的接受參數的方式@RequestBody ,但必須將參數放置在@RequestBody中
針對PathVariable 需要注意的是參數中包含特殊字符的問題,可能導致參數不全。
對于各種請求方式,驗證一下當前用戶,對url進行加密 是有必要的。(尤其是關鍵數據)
SpringMVC接收不到前端傳遞的參數原因
在學習SpringMvc的時候遇到了一個問題,后臺一直接收不到前臺傳遞過來的參數,耽誤了好長時間終于找到了原因,寫篇博客記錄下這個坑,嚶嚶嚶 --__–
代碼清單
使用SpringMvc接受前臺傳遞的參數非常簡單,只要參數名和前臺表單中的名稱一致即可,我弄得是一個文件上傳的例子,所以看下我的前臺頁面
1
2
3
4
5
6
7
8
9
10
|
< body > <!-- enctype="multipart/form-data"在文件上傳時加入,編碼類型,其值默認是application/x-www-form-urlencoded --> < form action = "testFileUpload" method = "post" enctype = "multipart/form-data" > File: < input type = "file" name = "file" /> Desc: < input type = "text" name = "desc" /> < input type = "submit" value = "Submit" /> </ form > < br >< br > < a href = "emps" rel = "external nofollow" >List All Employees</ a > </ body > |
下面是SpringMvc的控制器
1
2
3
4
5
6
7
8
9
10
11
|
@Controller public class springMVCTest { @RequestMapping ( "/testFileUpload" ) public String testFileUpload( @RequestParam ( "desc" ) String desc, @RequestParam ( "file" ) MultipartFile file) throws IOException { System.out.println( "desc: " + desc); System.out.println( "originalFilename: " + file.getOriginalFilename()); System.out.println( "inputStream: " + file.getInputStream()); return "success" ; } } |
接著是web.xml文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<!-- 配置DispatcherServlet --> <!-- SpringMvc會根據servlet-name配置,找到/WEB-INF/dispatcher-servlet.xml作為配置文件載入Web工程中 --> < servlet > < servlet-name >springDispatcherServlet</ servlet-name > < servlet-class >org.springframework.web.servlet.DispatcherServlet</ servlet-class > < init-param > < param-name >contextConfigLocation</ param-name > < param-value >classpath:springmvc.xml</ param-value > </ init-param > < load-on-startup >1</ load-on-startup > </ servlet > < servlet-mapping > < servlet-name >springDispatcherServlet</ servlet-name > < url-pattern >/</ url-pattern > </ servlet-mapping > |
** 然后是SpringMvc的配置文件**
1
2
3
4
5
6
7
8
9
|
<!-- 配置自動掃描的包 --> < context:component-scan base-package = "com.zgz.springmvc.crud" ></ context:component-scan > < context:component-scan base-package = "com.zgz.springmvc.test" ></ context:component-scan > <!-- 配置視圖解析器 --> < bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver" > < property name = "prefix" value = "/WEB-INF/views/" ></ property > < property name = "suffix" value = ".jsp" ></ property > </ bean > |
之后坑就來了,由于是文件上傳,所以需要在Spring MVC的配置文件中添加multipartResolver,添加就添加唄,于是我就加上了下面這一段代碼:
1
2
3
4
5
6
|
<!-- 配置 MultipartResolver --> < bean id = "commonsMultipartResolver" class = "org.springframework.web.multipart.commons.CommonsMultipartResolver" > < property name = "defaultEncoding" value = "UTF-8" ></ property > < property name = "maxUploadSize" value = "1024000" ></ property > </ bean > |
然后坑就出現,費盡周折發現是id寫錯了,id=“multipartResolver”,修改代碼為:
1
2
3
4
5
|
< bean id = "multipartResolver" class = "org.springframework.web.multipart.commons.CommonsMultipartResolver" > < property name = "defaultEncoding" value = "UTF-8" ></ property > < property name = "maxUploadSize" value = "1024000" ></ property > </ bean > |
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/qq_40406929/article/details/114733221