SpringBoot第四課——常用傳參方式

接着第一課:https://blog.csdn.net/sinat_22808389/article/details/81590042

1.path傳參 

在RestController 下添加如下方法:

//path 傳參數 get請求方式
    @RequestMapping(value = "/postPath")
    public String postByPath(@RequestParam(value = "id")Integer id) {
    	System.out.println(id);
    	return "id" + id;
    }

瀏覽器輸入http://localhost:8080/postPath?id=2 

可在後臺看到打印的id爲2,(這裏也可以不要@RequestParam(value = "id"),也可傳參進來)

2.ajax通過json傳參:

//接收json參數  同時json轉爲java對象
    @CrossOrigin(origins="*",allowCredentials="true")
    @RequestMapping(value = "/postJson",method = RequestMethod.POST)
    public String postByJson(@RequestBody User user) {
    	System.out.println(user.toString());
    	return "post success!" + user;
    }

User類如下:

public class User {

	private String name;
	private String age;
	private String addr;
// get() set() toString()..

}

前端postjson.html如下:


<!DOCTYPE html>
<html >
<head>
<meta charset="utf-8">
<title>crossdomain</title>
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js">
</script>
<script type="text/javascript" src="http://echarts.baidu.com/gallery/vendors/echarts/echarts.min.js"></script>
</head>
<body >
   <div id = "data" ></div>
<script type="text/javascript">
var data = JSON.stringify({name:"xyp",age:18});
//ajax提交數據
$.ajax({
    async:false,
    type:"post",
    url:"http:localhost:8080/postJson",
    data:data, 
    contentType: "application/json;charset=UTF-8",
    dataType:"json", 
    success:function(data){
       $("#data").text(data);
    },
    error:function(){
        $("#data").text("數據請求失敗");
    }
    });
   
</script>
</body>
</html>

 瀏覽器運行postjson.html,可看到後臺如下

 

 可看到name,age都傳進來了,但是addr爲空的,這是爲什麼呢?這是因爲前端傳的只有name,age,這裏的json轉java是通過反射去賦值的,json裏沒有addr所以就不會去賦值,但是如果json裏有的屬性而java對象沒有,可想而知會報錯,因爲java通過反射去設置值的時候找不到那個屬性。

 3.ajax提交表單:

postForm.html:

<!DOCTYPE html>
<html style="height: 100%; margin: 0">
<head>
<meta charset="utf-8">
<title>crossdomain</title>
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js">
</script>
<script type="text/javascript" src="http://echarts.baidu.com/gallery/vendors/echarts/echarts.min.js"></script>
</head>
<body >
   <form id = "form" >
    <input type="text" name="name" value="xyp"></input> 
    <input type="text" name="age" value="18"></input> 
   </form>
   <div id = "data"></data>
<script type="text/javascript">
//ajax 提交form
$.ajax({
    async:false,
    type:"post",
    url:"http:localhost:8080/postForm",
    data:$("#form").serialize(), 
    contentType: "application/json;charset=UTF-8",
    success:function(data){
       $("#data").text(data);
    },
    error:function(){
        $("#data").text("error");
    }
    });
   
</script>
</body>
</html>

後端在RestController添加如下方法:

   @CrossOrigin(origins="*",allowCredentials="true")
    @RequestMapping(value = "/postForm",method = RequestMethod.POST)
    public String postByForm(@RequestBody String user) {
    	System.out.println(user.toString());
    	return user.toString();
    }

瀏覽器打開 postForm.html可在後臺看到:

 

這裏不能用@RequestBody User user來接收,因爲傳過來的不是json格式的參數。

更詳細的參數傳遞可參考:https://www.cnblogs.com/harrychinese/p/springboot_mvc_view_function.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章