這篇文章主要介紹“javaweb前端向后端傳值的方式有哪些”,在日常操作中,相信很多人在javaweb前端向后端傳值的方式有哪些問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”javaweb前端向后端傳值的方式有哪些”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
1.查詢字符串的方式
即在請求地址后拼接上請求參數,多個參數以&連接- 表單方式提交
2.第一種方式是在表單中直接提交,第二種方式是通過ajax方式,data屬性是js對象或者json對象,不指明contentType默認就是以表單方式提交。
3.json字符串方式
后端以@RequestBody方式接受,以對象形式接受,可以和查詢字符串混用,除了對象之外后端還可以接受查詢字符串參數。
經測試,后端不加@RequestBody,json字符串傳到后臺就不能正確匹配對象。
@RequestParam
@PathVariable
@RequestBody
@RequestParam:將請求參數綁定到你控制器的方法參數上(是springmvc中接收普通參數的注解)。接收的參數是來自HTTP請求體或請求url的參數串中。。
語法:@RequestParam(value=”參數名”,required=”true/false”,defaultValue=””)
value:為接收url的參數名(相當于key值)。
required:是否包含該參數,默認為true,表示該請求路徑中必須包含該參數,如果不包含就報錯。
defaultValue:默認參數值,如果設置了該值,required=true將失效,自動為false,如果沒有傳該參數,就使用默認值。
@RequestParam用來處理 Content-Type 為 application/x-www-form-urlencoded 編碼的內容,Content-Type默認為該屬性。
@RequestParam也可用于其它類型的請求,例如:POST、DELETE等請求。
代碼如下:
1.get請求參數帶在url中。
前端代碼:
<button><a href="/annotation/paramGet?name=tom&age=15" target="_blank">點我發送get請求</a></button>
后端代碼:
@RequestMapping(value = "paramGet",produces = "application/json;charset=utf-8")
@ResponseBody
public String paramGet(@RequestParam("name")String username,
@RequestParam("age")int age,
@RequestParam(value = "score",required = false)Float score){
return username+"今年"+age+"歲"+",考試得了"+score+"分!";
}2.post請求參數帶在url或者請求體中
前端代碼:
<button onclick="paramPost()">點我發送post請求</button>
<script type="text/javascript">
//post請求參數帶在url中。
function paramPost() {
$.ajax({
type:"post",
//參數在url中,載荷是查詢字符串,就是沒有請求體
// url:"paramPost?name=jack&age=25",
url:"paramPost",
//參數在請求體中,js對象和json對象都可以提交,默認是提交表單數據
data:{name:"jack",
age:15
},
dataType:"json",
success: function(data){
console.log(data);
alert(data);
alert(data.sss);
}
});
}
</script>后端代碼:
@RequestMapping(value = "paramPost",produces = "application/json;charset=utf-8",method = RequestMethod.POST)
@ResponseBody
public Map<String,String> paramPost(@RequestParam("name")String username,
@RequestParam("age")int age,
@RequestParam(value = "score",required = false)Float score){
Map<String,String> map = new HashMap<>();
String ss = username+"今年"+age+"歲"+",考試得了"+score+"分!";
map.put("sss",ss);
return map;
}3.直接以表單方式提交
前端代碼:
<form action="<%=basePath%>annotation/paramPost" method="post"> 姓名:<input type="text" name="name" required="required"><br> 年齡:<input type="text" name="age" required="required"><br> 分數:<input type="text" name="score"><br> <input type="submit"> </form>
后端代碼:
@RequestMapping(value = "paramPost",produces = "application/json;charset=utf-8",method = RequestMethod.POST)
@ResponseBody
public Map<String,String> paramPost(@RequestParam("name")String username,
@RequestParam("age")int age,
@RequestParam(value = "score",required = false)Float score){
Map<String,String> map = new HashMap<>();
String ss = username+"今年"+age+"歲"+",考試得了"+score+"分!";
map.put("sss",ss);
return map;
}上面這種方式提交就是表單方式,它和通過ajax方式,data屬性是js或者json對象,不指明contentType是一樣的。
Web應用中的URL通常不是一成不變的,例如微博兩個不同用戶的個人主頁對應兩個不同的URL:http://weibo.com/user1和http://weibo.com/user2。 我們不能對于每一個用戶都編寫一個被@RequestMapping注解的方法來處理其請求,也就是說,對于相同模式的URL(例如不同用戶的主頁,他們僅僅是URL中的某一部分不同, 為他們各自的用戶名,我們說他們具有相同的模式)。
可以在@RequestMapping注解中用{ }來表明它的變量部分,例如:
@RequestMapping(value="/user/{username}")需要注意的是,在默認情況下,變量中不可以包含URL的分隔符/,例如路由不能匹配/user/Denny/Jon,即使你認為Denny/Jon是一個存在的用戶名。
在路由中定義變量規則后,通常我們需要在處理方法(也就是@RequestMapping注解的方法)中獲取這個URL的具體值,并根據這個值(例如用戶名)做相應的操作, SpringMVC提供的@PathVariable可以幫助我們:
@RequestMapping(value="/user/{name}")
public String userProfile(@PathVariable(value="name") String username) {
return "user"+username;
}PathVariable加RequestParam的組合方式:
前端代碼:
<button><a href="<%=basePath%>annotation/pathTest/david/16?score=19.5" target="_blank">點我發送path請求</a></button>
后端代碼:
@RequestMapping(value = "pathTest/{name}/{age}",produces = "application/json;charset=utf-8")
@ResponseBody
public Map<String,String> pathTest(@PathVariable("name")String username,
@PathVariable("age")int age,
@RequestParam(value = "score",required = false)Float score){
Map<String,String> map = new HashMap<>();
String ss = username+"今年"+age+"歲"+",考試得了"+score+"分!";
map.put("sss",ss);
return map;
}@RequestBody主要用來接收前端傳遞給后端的json字符串中的數據的(請求體中的數據的);而最常用的使用請求體傳參的無疑是POST請求了, 所以使用@RequestBody接收數據時,一般都用POST方式進行提交。在后端的同一個接收方法里,@RequestBody與@RequestParam()可以同時使用, @RequestBody最多只能有一個,而@RequestParam()可以有多個。
注:一個請求,只有一個RequestBody;一個請求,可以有多個RequestParam。
前端代碼:
<button onclick="requestBodyTest()">點我測試requestbody</button>
<script type="text/javascript">
//測試requestbody
function requestBodyTest() {
let json = {"uName":"david","phone":13887898998};
alert(typeof json);
$.ajax({
type:"post",
url:"<%=basePath%>annotation/requestBodyTest?score=17.8",
//json字符串
data:JSON.stringify(json),
//指定發送數據的格式
contentType:"application/json", //是這種格式,不是json/application
//指定返回數據的格式
dataType:"json",
success: function(data){
console.log(typeof data);
console.log(data);
alert(typeof data);
alert(data.sss);
}
});
}
</script>后端代碼:
@RequestMapping(value = "requestBodyTest",produces = "application/json;charset=utf-8",method = RequestMethod.POST)
@ResponseBody
public Map<String,String> requestBodyTest(@RequestBody User user,
@RequestParam(value = "score",required = false)Float score){
Map<String,String> map = new HashMap<>();
String ss = user.getuName()+"今年"+"考試得了"+score+"分!";
map.put("sss",ss);
return map;
}到此,關于“javaweb前端向后端傳值的方式有哪些”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。