文章目录
前端视图层
前端视图中使用链接<a>
或者表单<form>
或者axios来发送请求。
1. 使用链接发送请求
<a th:href="@{/test(username='admin',password=123456)}">测试</a>
2. 使用表单发送请求
使用表单<form>
,且method可以设置为get或post。
- method为get
<form th:action="@{/test}" method="get">
username:<input type="text" name="username" /><br/>
password:<input type="password" name="password"/><br/>
<input type="submit" value="提交"/>
</form>
2. method为post
<form th:action="@{/test}" method="post">
username:<input type="text" name="username" /><br/>
password:<input type="password" name="password"/><br/>
<input type="submit" value="提交"/>
</form>
3. 使用axios发送请求
<body>
<button onclick="handleClick()">点击测试</button>
<script th:src="@{/static/js/axios.min.js}"></script>
<script th:inline="javascript">
//获取上下文路径
var contextPath = /*[[@{/}]]*/'';
function handleClick(){
axios({
method:"get",
// method:"post",
url:contextPath+"test",
params:{
username:"admin",
password:"123456"
}
})
}
</script>
</body>
后端控制层
后端控制层获取请求参数有四种方式。
1. 通过Servlet API方式
// @RequestMapping(value = "/test",method = RequestMethod.POST)
@RequestMapping(value = "/test",method = RequestMethod.GET)
public String test(HttpServletRequest request){
String username = request.getParameter("username");
String password = request.getParameter("password");
System.out.println(username+","+password);
return "success";
}
10:06:32.279 [http-nio-8080-exec-5] DEBUG org.springframework.web.servlet.DispatcherServlet - GET "/demo6/test?username=admin&password=123456", parameters={masked}
10:06:32.282 [http-nio-8080-exec-5] DEBUG org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped to com.example.mvc.controller.TestController#test(HttpServletRequest)
admin,123456
2. 控制器方法形参名称与请求参数名称一致。
// @RequestMapping(value = "/test",method = RequestMethod.POST)
@RequestMapping(value = "/test",method = RequestMethod.GET)
public String test(String username,String password){
System.out.println(username+","+password);
return "success";
}
09:50:14.278 [http-nio-8080-exec-4] DEBUG org.springframework.web.servlet.DispatcherServlet - GET "/demo6/test?username=admin&password=123456", parameters={masked}
09:50:14.280 [http-nio-8080-exec-4] DEBUG org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped to com.example.mvc.controller.TestController#test(String, String)
admin,123456
3. 使用@RequestParam
控制器方法形成名称与请求参数名称不一致,使用@RequestParam建立二者间的映射关系
// @RequestMapping(value = "/test",method = RequestMethod.POST)
@RequestMapping(value = "/test",method = RequestMethod.GET)
public String test(@RequestParam("username") String name,@RequestParam("password") String passwd){
System.out.println(name+","+passwd);
return "success";
}
10:01:27.103 [http-nio-8080-exec-5] DEBUG org.springframework.web.servlet.DispatcherServlet - GET "/demo6/test?username=admin&password=123456", parameters={masked}
10:01:27.107 [http-nio-8080-exec-5] DEBUG org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped to com.example.mvc.controller.TestController#test(String, String)
admin,123456
4. 控制器方法形参是一个实体类
控制器方法形参是一个实体类,且实体类属性名称与请求参数名称一致。
package com.example.mvc.bean;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private Integer id;
private String username;
private String password;
}
// @RequestMapping(value = "/test",method = RequestMethod.POST)
@RequestMapping(value = "/test",method = RequestMethod.GET)
public String test(User user){
System.out.println(user);
return "success";
}
10:03:54.520 [http-nio-8080-exec-7] DEBUG org.springframework.web.servlet.DispatcherServlet - GET "/demo6/test?username=admin&password=123456", parameters={masked}
10:03:54.523 [http-nio-8080-exec-7] DEBUG org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped to com.example.mvc.controller.TestController#test(User)
User(id=null, username=admin, password=123456)
5. 使用RequestEntity(仅用于获取查询字符串)
@RequestMapping(value = "/test",method = RequestMethod.GET)
public String test(RequestEntity<String> requestEntity){
URI uri = requestEntity.getUrl();
String query = uri.getQuery();
String[] arr = query.split("&");
for(String el:arr){
String[] keyValues= el.split("=");
String key = keyValues[0];
String value = keyValues[1];
System.out.println(key+"="+value);
}
return "success";
}
10:30:37.640 [http-nio-8080-exec-5] DEBUG org.springframework.web.servlet.DispatcherServlet - GET "/demo6/test?username=admin&password=123456", parameters={masked}
10:30:37.641 [http-nio-8080-exec-5] DEBUG org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped to com.example.mvc.controller.TestController#test(RequestEntity)
10:30:37.661 [http-nio-8080-exec-5] DEBUG org.springframework.web.servlet.mvc.method.annotation.HttpEntityMethodProcessor - Read "application/octet-stream" to []
username=admin
password=123456
6. 使用@RequestBody(不接受get请求,接受post请求,且必须包含请求体)
使用@RequestBody时,不接受get请求,否则报错:Required request body is missing。
使用@RequestBody时,可以接受post请求,且必须包含请求体数据。
- 比如,使用form发送post请求,控制器方法形参是String类型。
<form th:action="@{/test}" method="post">
username:<input type="text" name="username" /><br/>
password:<input type="password" name="password"/><br/>
<input type="submit" value="提交"/>
</form>
@RequestMapping(value = "/test",method = RequestMethod.POST)
public String test(@RequestBody String requestBody){
System.out.println(requestBody);
return "success";
}
14:27:15.741 [http-nio-8080-exec-6] DEBUG org.springframework.web.servlet.DispatcherServlet - POST "/demo6/test", parameters={masked}
14:27:15.744 [http-nio-8080-exec-6] DEBUG org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped to com.example.mvc.controller.TestController#test(String)
14:27:15.767 [http-nio-8080-exec-6] DEBUG org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor - Read "application/x-www-form-urlencoded;charset=UTF-8" to ["username=admin&password=123456"]
username=admin&password=123456
- 比如,使用axios发送post请求,控制器方法形参是String类型。
<body>
<button onclick="handleClick()">点击测试</button>
<script th:src="@{/static/js/axios.min.js}"></script>
<script th:inline="javascript">
//获取上下文路径
var contextPath = /*[[@{/}]]*/'';
function handleClick(){
axios({
method:"post",
url:contextPath+"test",
data:{
"username":"admin",
"password":"123456"
}
})
}
</script>
</body>
@RequestMapping(value = "/test",method = RequestMethod.POST)
public String test(@RequestBody String requestBody){
System.out.println(requestBody);
return "success";
}
14:28:02.357 [http-nio-8080-exec-2] DEBUG org.springframework.web.servlet.DispatcherServlet - POST "/demo6/test", parameters={}
14:28:02.357 [http-nio-8080-exec-2] DEBUG org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped to com.example.mvc.controller.TestController#test(String)
14:28:02.358 [http-nio-8080-exec-2] DEBUG org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor - Read "application/json;charset=UTF-8" to ["{"username":"admin","password":"123456"}"]
{"username":"admin","password":"123456"}
- 比如,使用axios发送post请求,控制器方法形参是一个实体类。
<body>
<button onclick="handleClick()">点击测试</button>
<script th:src="@{/static/js/axios.min.js}"></script>
<script th:inline="javascript">
//获取上下文路径
var contextPath = /*[[@{/}]]*/'';
function handleClick(){
axios({
method:"post",
url:contextPath+"test",
data:{
"username":"admin",
"password":"123456"
}
})
}
</script>
</body>
package com.example.mvc.bean;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private Integer id;
private String username;
private String password;
}
@RequestMapping(value = "/test",method = RequestMethod.POST)
public String test(@RequestBody User user){
System.out.println(user);
return "success";
}
14:33:59.565 [http-nio-8080-exec-6] DEBUG org.springframework.web.servlet.DispatcherServlet - POST "/demo6/test", parameters={}
14:33:59.566 [http-nio-8080-exec-6] DEBUG org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped to com.example.mvc.controller.TestController#test(User)
14:33:59.678 [http-nio-8080-exec-6] DEBUG org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor - Read "application/json;charset=UTF-8" to [User(id=null, username=admin, password=123456)]
User(id=null, username=admin, password=123456)