SpringBoot2 Controller接收参数的方式
程序员文章站
2024-03-23 17:59:34
...
Controller
package com.zz.controller;
import java.util.HashMap;
import java.util.Map;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.zz.entity.User;
@RestController
@RequestMapping("test")
public class TestController {
//路径传值
@RequestMapping("t1/{p1}")
public Map T1(@PathVariable("p1") String parameter1){
Map map=new HashMap();
map.put("rs", parameter1);
return map;
}
//?传值
@RequestMapping("t2")
public Map T2(@RequestParam("p2") String parameter2){
Map map=new HashMap();
map.put("rs", parameter2);
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());
map.put("rs2", person.getPwd());
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);
map.put("rs2",myCookie);
return map;
}
// 表单的参数写在Controller相应的方法的形参中
// 适用于get方式提交,不适用于post方式提交。
@RequestMapping("t6")
public Map t6(String name,String pwd){
Map map=new HashMap();
map.put("rs", name);
map.put("rs2",pwd);
return map;
}
}
HTML
<!DOCTYPE html>
<html>
<head>
<title>Bootstrap 实例</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/css/bootstrap.min.css">
<script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/js/bootstrap.min.js"></script>
<script src="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/js/bootstrap.bundle.min.js"></script>
</head>
<body>
<div class="container">
<h2>测试</h2>
<button type="button" id="t1" class="btn">t1</button>
<button type="button" id="t2" class="btn btn-primary">t2</button>
<button type="button" id="t3" class="btn btn-secondary">t3</button>
<button type="button" id="t4" class="btn btn-success">t4</button>
<button type="button" id="t5" class="btn btn-info">t5</button>
<button type="button" id="t6" class="btn btn-warning">t6</button>
</div>
<script type="text/javascript">
$(document).ready(function(){
$("#t1").click(function(){
$.get("test/t1/jacky", function(data){
alert("Data Loaded: " + data.rs);
});
})
$("#t2").click(function(){
$.get("test/t2?p2=Kangkang", function(data){
alert("Data Loaded: " + data.rs);
});
})
$("#t3").click(function(){
var data={"name":"LinHao","pwd":"123"}
$.ajax({
url:"test/t3",
async:false,
type:"POST",
contentType:"application/json",
dateType:"json",
data:JSON.stringify(data),
success:function(data){
alert("Data Loaded: " + data.rs);
}
});
})
$("#t4").click(function(){
$.ajax({
url:"test/t4?name=jacky&pwd=123",
async:false,
type:"POST",
contentType:"form-data",
dateType:"json",
success:function(data){
alert("Data Loaded: " + data.rs+data.rs2);
}
});
})
//暂时不通
$("#t5").click(function(){
var data={"name":"LinHao","pwd":"123"}
$.ajax({
url:"test/t5",
async:false,
type:"POST",
contentType:"application/json",
dateType:"json",
headers:{
'Header':"from header",
'Cookie':'sdfs'
},
data:JSON.stringify(data),
success:function(data){
alert("Data Loaded: " + data.rs+data.rs2);
}
});
})
$("#t6").click(function(){
$.get("test/t6?name=Kangkang&pwd=567", function(data){
alert("Data Loaded: " + data.rs);
});
})
});
</script>
</body>
</html>
上一篇: PythonDay1---if、while、for循环函数以及字符串的用法
下一篇: c中计算门票
推荐阅读
-
SpringBoot2 Controller接收参数的方式
-
SpringMVC接收前端页面参数的4种方式
-
c语言宏定义与普通函数接收参数的区别--值得注意
-
SpringMVC系列之参数传递【前后端参数传递的方式详解】
-
java参数的传递方式 博客分类: java学习 java
-
java参数的传递方式 博客分类: java学习 java
-
SpringMVC的各种参数绑定方式 SpringMVC绑定表单
-
spring mvc 用map的形式接收form表单的参数: 博客分类: springmvc
-
window.location.href以post方式传递参数的方法 博客分类: web前端 hrefpostget
-
Java Spring Controller 获取请求参数的几种方法详解