如何在JSP中处理@RequestParam?及如何从客户端获取@RestController中的@RequestParam?
问题1:如何在JSP文件中处理@RequestParam?
首先得明确一个关键点:@RequestParam是Spring MVC专为Controller层(Java代码)设计的注解,JSP作为视图层不会直接使用这个注解。不过咱们可以通过两种方式在JSP里获取到对应请求参数:
场景1:Controller用@RequestParam接收后传给JSP
举个实际例子,假设你的Controller代码是这样的:
@Controller @RequestMapping("/demo") public class DemoController { @GetMapping("/test") public String testPage(@RequestParam(name = "username", required = false) String username, Model model) { // 把接收的参数存到Model里,就能传递给JSP了 model.addAttribute("username", username); return "test-page"; // 跳转到test-page.jsp } }
那在对应的test-page.jsp里,推荐用EL表达式直接取值(简洁又规范):
<p>收到的用户名:${username}</p>
要是你习惯用老的JSP脚本(现在不太推荐了),也可以这么写:
<% String username = request.getParameter("username"); out.println("收到的用户名:" + username); %>
场景2:JSP直接从请求中获取参数
如果不需要Controller中转,JSP也能直接通过request.getParameter()拿到请求参数——这和Controller用@RequestParam做的事情本质是一样的:
<% // 注意getParameter返回的是String类型,要转数字得自己处理 String ageStr = request.getParameter("age"); Integer age = null; if (ageStr != null && !ageStr.isEmpty()) { age = Integer.parseInt(ageStr); } out.println("年龄:" + age); %>
如果是复选框这类多值参数,就用request.getParameterValues():
<% String[] hobbies = request.getParameterValues("hobby"); if (hobbies != null) { out.println("你的爱好:"); for (String hobby : hobbies) { out.print(hobby + " "); } } %>
问题2:在@RestController中用@RequestParam获取客户端参数,兼分析你的代码问题
先看你贴的这段代码:
@ResponseBody @RequestMapping(method = RequestMethod.GET) public ResponseEntity<List<ProjectBean>> getAllBeans(@RequestParam(name = "head") Integer headId) { Integer head = securityService.getLoggedAccountId(); List<ProjectBean> projects = (List<ProjectBean>) projectService.getByHead(head); return new ResponseE... }
先讲正确的用法,再说说你代码里的小问题:
一、正确在@RestController中获取@RequestParam参数
首先,@RestController已经自带了@ResponseBody的功能,所以你不需要额外加这个注解,代码可以简化成这样:
@RestController @RequestMapping("/projects") // 建议加个基础路径,让接口更规范 public class ProjectController { @GetMapping // 这和@RequestMapping(method = RequestMethod.GET)是完全等价的,更简洁 public ResponseEntity<List<ProjectBean>> getAllBeans( // @RequestParam的几个常用属性给你梳理下: // name:指定要接收的请求参数名称 // required:是否必填,默认是true,客户端不传会直接报400错误;设为false的话不传就是null // defaultValue:如果参数没传,就用这个默认值 @RequestParam(name = "head", required = false) Integer headId) { // 这里直接用headId就行,它就是客户端传过来的参数值 List<ProjectBean> projects = projectService.getByHead(headId); return ResponseEntity.ok(projects); // 返回200状态码+数据,比手动new ResponseEntity更简洁 } }
客户端传递参数的方式(因为是GET请求):
- 直接拼在URL后面:
http://你的域名/projects?head=123 - 如果是表单GET提交,把input的name设为"head",提交后会自动拼到URL里
二、你的代码里的问题点
你在方法参数里用@RequestParam(name = "head")接收了headId,但后面完全没用到它,反而用securityService.getLoggedAccountId()获取了head来调用服务——这会导致两个问题:
- 客户端传了
head参数也白传,根本没用到 - 因为
@RequestParam默认required=true,如果客户端不传head参数,请求会直接报错(400 Bad Request)
针对不同需求,给你两个修改方向:
需求1:优先用客户端传的head参数,没传就用登录用户ID
@GetMapping public ResponseEntity<List<ProjectBean>> getAllBeans( @RequestParam(name = "head", required = false) Integer headId) { // 确定要使用的ID:客户端传了就用传的,否则用登录用户ID Integer targetHeadId = (headId != null) ? headId : securityService.getLoggedAccountId(); List<ProjectBean> projects = projectService.getByHead(targetHeadId); return ResponseEntity.ok(projects); }
需求2:不需要客户端传参数,直接用登录用户ID
那直接去掉@RequestParam注解就行,避免强制要求客户端传参数:
@GetMapping public ResponseEntity<List<ProjectBean>> getAllBeans() { Integer head = securityService.getLoggedAccountId(); List<ProjectBean> projects = projectService.getByHead(head); return ResponseEntity.ok(projects); }
内容的提问来源于stack exchange,提问作者Kseniya Korzun
相关产品推荐
相关产品推荐

