当前位置:   article > 正文

SpringMVC接收参数方式,Controller接收参数,常见不规范的传参和错误传参_controller获取不到参数

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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
@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;
	}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

方式二:在形参列表中直接写前台需要注入的参数,基于同名规则进行注入的;

<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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
@RequestMapping("/value2")
	ModelAndView value2(String username, String password) throws Exception{
		User u=new User(username, password);
		System.out.println(u);
		return null;
	}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

如果前台名字和后台形参名字不一致的情况,使用@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;
	}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

方式三:使用模型传参数的方式(采用属性注入形式)

<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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
@RequestMapping("/value4")
	ModelAndView value4(User u) throws Exception{
		System.out.println(u);
		return null;
	}
  • 1
  • 2
  • 3
  • 4
  • 5

方式四:使用地址栏传参方式

  • 请求参数
http://localhost:8080/temp/6
  • 1
@RequestMapping("/temp/{abc}")
	ModelAndView value5(@PathVariable("abc")String name) throws Exception{
		System.out.println(name);
		return null;
	}
  • 1
  • 2
  • 3
  • 4
  • 5

方式五: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
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
[1,2,5]
  • 1

在这里插入图片描述

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);
		}
	
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

在这里插入图片描述
在这里插入图片描述

常见不合规错误的传参

Controller 方法使用了@RequestParam 注解传参,如果没有给这个参数传入值,将导致报错。因为默认 required 为 true。

	@GetMapping("/queryMenuDetailById")
    public ResponseEntity<BasiccBfmMenu> queryById(@RequestParam Long id);
  • 1
  • 2
http://localhost:8080/queryMenuDetailById
  • 1

前端请求传入后端不接收的参数,该参数会自动被过滤不会报错

	@GetMapping("/queryMenuDetailById")
    public ResponseEntity<BasiccBfmMenu> queryById(Long id);
  • 1
  • 2
http://localhost:8080/queryMenuDetailById?id=3&ljj=2434
  • 1
  • 请求存在@RequestBody,即使是不传入任何参数
    @PostMapping("/editDir")
    public ResultVO editDir(@RequestBody DirDto dirDto);
  • 1
  • 2

在这里插入图片描述

直接用String接收json,json写法有问题

public R<Boolean> delete(@RequestBody String id) 
  • 1
{"id":"1527312204482969600"}
  • 1

这个时候id的值将会是 {“id”:“1527312204482969600”} 而不是1527312204482969600

参考文档

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/302736
推荐阅读
相关标签
  

闽ICP备14008679号