地址传值
@PathVariable
获取路径参数。即url/{id}这种形式。
? 传值
@RequestParam
获取查询参数。即url?name=这种形式
用注解@RequestParam绑定请求参数到方法入参
当请求参数username不存在时会有异常发生,可以通过设置属性required=false解决,例如: @RequestParam(value=“username”, required=false)
Body参数
//application/jason
@PostMapping(path = “/demo1”)
public void demo1(@RequestBody Person person) {
无注解(form提交)
//form-data
@PostMapping(path = “/demo1”)
public void demo1(Person person) {
请求头参数以及Cookie
@RequestHeader
2、@CookieValue
例子
java代码:
@GetMapping("/demo3")
public void demo3(@RequestHeader(name = “myHeader”) String myHeader,
@CookieValue(name = “myCookie”) String myCookie) {
System.out.println(“myHeader=” + myHeader);
System.out.println(“myCookie=” + myCookie);
也可以这样
@GetMapping("/demo3")
public void demo3(HttpServletRequest request) {
System.out.println(request.getHeader(“myHeader”));
for (Cookie cookie : request.getCookies()) {
if (“myCookie”.equals(cookie.getName())) {
System.out.println(cookie.getValue());
//请求头参数以及Cookie
@RequestMapping("request5")
public String test5(@RequestHeader(name ="Header") String Header,
@CookieValue(name ="cookie1") String cookie1,
@CookieValue(name ="cookie2") String cookie2){
return "Header:"+Header+"cookie1:"+cookie1+"cookie2"+cookie2;
}
1
2
3
4
5
6
7
//Cookie
@GetMapping("/ee")
public String ee(@RequestHeader(name="myHeader") String myHeader,@CookieValue(name="myCookie") String myCookie){
System.out.println("myHeader=" + myHeader);
System.out.println("myCookie=" + myCookie);
return "---------"+myHeader+"============="+myCookie;
}
1
2
3
4
5
6
7
表单的参数写在Controller相应的方法的形参中
适用于get方式提交,不适用于post方式提交。
/**
* 1.直接把表单的参数写在Controller相应的方法的形参中
* @param username
* @param password
* @return
*/
@RequestMapping("/addUser1")
public String addUser1(String username,String password) {
System.out.println(“username is:”+username);
@ModelAttribute注解获取POST请求的FORM表单数据
/**
* 使用@ModelAttribute注解获取POST请求的FORM表单数据
* @param user
* @return
*/
@RequestMapping(value="/addUser5",method=RequestMethod.POST)
public String addUser5(@ModelAttribute(“user”) UserModel user) {
System.out.println(“username is:”+user.getUsername());
System.out.println(“password is:”+user.getPassword());
return “demo/index”;
测试代码
Controller
package com.zz.controller;
import java.util.HashMap;
import java.util.Map;
import com.zz.entity.User;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("test")
public class TestController {
//路径传值
@RequestMapping("t1/{p1}")
public Map t1(@PathVariable("p1") String paramter1){
Map map=new HashMap();
map.put("rs",paramter1);
return map;
}
//? 传值
@RequestMapping("t2")
public Map t2(@RequestParam("p1") String paramter1)
{
System.out.println(paramter1);
Map map=new HashMap();
map.put("rs",paramter1);
return map;
}
//Body参数
//application/jason
@RequestMapping("t3")
public Map t3(@RequestBody User person){
Map map=new HashMap();
map.put("rs",person.getName());
return map;
}
// 无注解(form提交)
//form-data
@RequestMapping("t4")
public Map t4(User person){
Map map=new HashMap();
map.put("rs",person.getName());
return map;
}
// 请求头参数以及Cookie
@RequestMapping("t5")
public Map t5(@RequestHeader(name ="myHeader") String myHeader,
@CookieValue(name = "myCookie") String myCookie){
System.out.println("myHeader=" + myHeader);
System.out.println("myCookie=" + myCookie);
Map map=new HashMap();
map.put("rs",myHeader);
return map;
}
// 表单的参数写在Controller相应的方法的形参中
// 适用于get方式提交,不适用于post方式提交。
@RequestMapping("t6")
public Map t6(String name,String pwd){
Map map=new HashMap();
map.put("rs",name);
return map;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
HTML
测试
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
SpringBoot Form post 提交 跳转失败 解决办法
报错日志:tSupportedException: Request method ‘POST’ not supported
解决:post controller方法里面 通过 return “redirect:/index/toIndex”; 执行get的跳转controller
package com.zz.controller;/**
* @Description: 描述
* @Author: Bsea
* @CreateDate: ${Date}
*/
import com.zz.config.DataValidationException;
import com.zz.entity.Member;
import com.zz.form.MemberForm;
import com.zz.service.MemberService;
import com.zz.util.FormUtil;
import com.zz.util.ResultVOUtil;
import com.zz.vo.ResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.validation.Valid;
/**
* @Description: java类作用描述
* @Author: Bsea
* @CreateDate: 2019/10/16$ 21:27$
*/
@Api(value = "会员控制器")
@Controller
@RequestMapping("index")
public class IndexController {
@Resource
MemberService memberService;
@ApiOperation(value = "添加会员", notes = "根据名字查找会员")
@ApiImplicitParam(name = "name", value = "会员名字", required = true, dataType = "String",paramType = "path")
@PostMapping("add")
public String add(@Valid MemberForm member, BindingResult bindingResult){
System.out.println(member);
if(bindingResult.hasErrors()){
throw new DataValidationException("验证错误");
}
Member member1=new Member();
BeanUtils.copyProperties(member,member1);
if(memberService.add(member1)!=null){
return "redirect:/index/toIndex";
}else{
return "redirect:/index/toIndex";
}
}
@RequestMapping("toIndex")
public String toIndex(){
return "/index2.html";
}
}
————————————————
版权声明:本文为CSDN博主「bseayin」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/h356363/article/details/90510609
本文分享自微信公众号 - Java全栈学习(taoquanquansh)。
如有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。