通过HttpServletRequest来获取前端页面参数,代码见下图 ,简单来说就是通过调用request的getParameter方法来获取参数
这种方式也可以获取表单参数,一般get和post请求都可以。此处只演示get请求。
控制器:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
package com.awaimai.web; import org.springframework.web.bind.annotation.*; import javax.servlet.http.HttpServletRequest; import java.util.HashMap; import java.util.Map; @RestController public class kzq { @RequestMapping (value = "/test2" ) public String test2(HttpServletRequest request) { String a = request.getParameter( "firstName" ); String b = request.getParameter( "lastName" ); return a+b; } } |
web访问:
===============================================================
// http://localhost:8080/test2?firstname=zhangsi&lastname=wanGwu @RequestMapping(value = "/test2") public String test2342(HttpServletRequest request) { String a = request.getParameter("firstname"); String b = request.getParameter("lastname"); return a+b; }