SpringMVC接收参数方式,Controller接收参数,常见不规范的传参和错误传参
方式一:使用servlet原生的方式,通过request.getParameter(“key”)获取参数;
<fieldset>
<legend>用户注册1</legend>
<form action="/value1" method="post">
用户名:<input type="text" name="username"/><br/>
密 码:<input type="password" name="password"/><br/>
<input type="submit" value="提交"/>
</form>
</fieldset>
@RequestMapping("/value1")
ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception{
String username=request.getParameter("username");
String password=request.getParameter("password");
User u=new User(username, password);
System.out.println(u);
return null;
}
方式二:在形参列表中直接写前台需要注入的参数,基于同名规则进行注入的;
<fieldset>
<legend>用户注册2</legend>
<form action="/value2" method="post">
用户名:<input type="text" name="username"/><br/>
密 码:<input type="password" name="password"/><br/>
<input type="submit" value="提交"/>
</form>
</fieldset>
@RequestMapping("/value2")
ModelAndView value2(String username, String password) throws Exception{
User u=new User(username, password);
System.out.println(u);
return null;
}
如果前台名字和后台形参名字不一致的情况,使用@RequestParam(“前台指定名称”)注解来进行处理:
@RequestMapping("/value3")
ModelAndView value3(@RequestParam("username")String name, String password) throws Exception{
User u=new User(name, password);
System.out.println(u);
return null;
}
方式三:使用模型传参数的方式(采用属性注入形式)
<fieldset>
<legend>用户注册4</legend>
<form action="/value4" method="post">
用户名:<input type="text" name="username"/><br/>
密 码:<input type="password" name="password"/><br/>
<input type="submit" value="提交"/>
</form>
</fieldset>
@RequestMapping("/value4")
ModelAndView value4(User u) throws Exception{
System.out.println(u);
return null;
}
方式四:使用地址栏传参方式
- 请求参数
http://localhost:8080/temp/6
@RequestMapping("/temp/{abc}")
ModelAndView value5(@PathVariable("abc")String name) throws Exception{
System.out.println(name);
return null;
}
方式五:Json传参,不能使用于get请求,一个请求只能有一个@RequestBody,也就是只能传一次json
@ResponseBody
@RequestMapping("test")
public void test( @RequestBody List<Integer> list) { //required=false 表示参数非必须
for (Integer integer : list) {
System.out.println(integer);
}
}
[1,2,5]
List类型传参
-
json方式请转到大标题,json传参(get请求不行)
-
下面这种请求方式传参同时适用于post请求和get请求
@ResponseBody
@RequestMapping("test")
public void test(@RequestParam(value="list",required = false) List<Integer> list) {
for (Integer integer : list) {
System.out.println(integer);
}
}
常见不合规错误的传参
Controller 方法使用了@RequestParam 注解传参,如果没有给这个参数传入值,将导致报错。因为默认 required 为 true。
@GetMapping("/queryMenuDetailById")
public ResponseEntity<BasiccBfmMenu> queryById(@RequestParam Long id);
http://localhost:8080/queryMenuDetailById
前端请求传入后端不接收的参数,该参数会自动被过滤不会报错
@GetMapping("/queryMenuDetailById")
public ResponseEntity<BasiccBfmMenu> queryById(Long id);
http://localhost:8080/queryMenuDetailById?id=3&ljj=2434
- 请求存在@RequestBody,即使是不传入任何参数
@PostMapping("/editDir")
public ResultVO editDir(@RequestBody DirDto dirDto);
直接用String接收json,json写法有问题
public R<Boolean> delete(@RequestBody String id)
{"id":"1527312204482969600"}
这个时候id的值将会是 {“id”:“1527312204482969600”} 而不是1527312204482969600
参考文档
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhgeikhe
系列文章
更多
同类精品
更多
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
excel下划线不显示怎么办
PHP中文网 06-23 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
怎样阻止微信小程序自动打开
PHP中文网 06-13 -
TikTok加速器哪个好免费的TK加速器推荐
TK小达人 10-01