由於使用到JSON,需要引入JSON依賴包
<!-- JSON包 -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.5</version>
</dependency>
在寫控制器前,先寫前端請求頁面
<!DOCTYPE html>
<html>
<head>
<!-- 加載Query文件-->
<script type="text/javascript" src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
<script type="text/javascript">
//使用JQuery傳遞JSON數據-@RequestBody處理
function sendJSON(){
var jsonData={
roleName: "梅西Messi",
note: "梅西note",
pagination:{
start: 1,
limit: 10,
}
}
$.ajax({
type: "POST",
url: "http://localhost:8080/Maven_SpringMVC/receiveJSON.do",
data: JSON.stringify(jsonData),
contentType: "application/json",
//成功後的方法
success: function(result){
alert("接收數據成功:"+result);
}
});
}
//使用JQuery傳遞數組-@RequestBody處理
function sendArray(){
var arrList = [3, 2, 9];
$.ajax({
type: "POST",
url: "http://localhost:8080/Maven_SpringMVC/receiveArray.do",
data: JSON.stringify(arrList),
contentType: "application/json",
//成功後的方法
success: function (result) {
alert("接收數據成功:"+result);
}
});
}
</script>
<meta name="content-type" content="text/html; charset=UTF-8">
<title>表單</title>
<style type="text/css">
.content{
display:flex;
}
.div{
padding-left:25px
}
</style>
</head>
<body>
<div class="content">
<div>
<form id="form" action="http://localhost:8080/Maven_SpringMVC/commonParams.do" method="POST">
<table border="1">
<caption>表單請求---提交後會請求到對應的URL上</caption>
<tr>
<td>名稱:</td>
<td><input id="roleName" name="roleName" /></td>
</tr>
<tr>
<td>備註:</td>
<td><input id="note" name="note" /></td>
</tr>
<tr>
<th colspan="2"><input id="commit" type="submit" value="提交" /></th>
</tr>
</table>
</form>
<br/>
<form id="form" action="http://localhost:8080/Maven_SpringMVC/commonParamsPojo.do" method="POST">
<table border="1">
<caption>表單請求---接收端使用pojo處理</caption>
<tr>
<td>名稱:</td>
<td><input id="roleName" name="roleName" /></td>
</tr>
<tr>
<td>備註:</td>
<td><input id="note" name="note" /></td>
</tr>
<tr>
<th colspan="2"><input id="commit" type="submit" value="提交" /></th>
</tr>
</table>
</form>
</div>
<div class="div">
<form id="form" action="http://localhost:8080/Maven_SpringMVC/requestParam.do" method="POST">
<table border="1">
<caption>表單請求-使用@RequestParam處理,注意name的屬性值</caption>
<tr>
<td>名稱:</td>
<td><input id="roleName" name="role_name" /></td>
</tr>
<tr>
<td>備註:</td>
<td><input id="note" name="note" /></td>
</tr>
<tr>
<th colspan="2"><input id="commit" type="submit" value="提交" /></th>
</tr>
</table>
</form>
</div>
<div class="div">
<a href="http://localhost:8080/Maven_SpringMVC/getRole/1.do">使用URL傳遞參數</a>
</div>
<div class="div">
<input type="button" οnclick="sendJSON()" value="使用JQuery傳遞JSON數據-@RequestBody處理"/>
</div>
<div class="div">
<input type="button" οnclick="sendArray()" value="使用JQuery傳遞數組-@RequestBody處理"/>
</div>
</div>
</body>
</html>
控制器
@Controller
public class ParamsController {
//控制器:接收普通參數,允許參數爲空
@RequestMapping("/commonParams")
public ModelAndView commonParams(String roleName, String note) {
System.out.println("進入普通參數控制器---表單提交會請求到對應的URL上,通過參數名稱和URL請求參數的名稱保持一致可自動獲取參數---允許參數爲空");
System.out.println("名稱:" + roleName);
System.out.println("備註:" + note);
ModelAndView mv = new ModelAndView();
mv.addObject("msg", "表單提交會請求到對應的URL上,通過參數名稱和URL請求參數的名稱保持一致可自動獲取參數---允許參數爲空");
mv.setViewName("paramsPage.jsp");
return mv;
}
//控制器:接收普通參數pojo處理方式,允許參數爲空
@RequestMapping("/commonParamsPojo")
public ModelAndView commonParamPojo(Role role) {
System.out.println("進入普通參數控制器--使用POJO定義的屬性和URL請求的名稱保存一致即可自動獲取參數,會自動保存到POJO對象裏---允許參數爲空");
System.out.println("名稱:" + role.getRoleName());
System.out.println("備註:" + role.getNote());
ModelAndView mv = new ModelAndView();
mv.addObject("msg", "使用POJO定義的屬性和URL請求的名稱保存一致即可自動獲取參數,會自動保存到POJO對象裏---允許參數爲空");
mv.setViewName("paramsPage.jsp");
return mv;
}
//控制器:使用@RequestParam註解接收參數,默認情況下該參數不能爲空,爲空時拋出異常
@RequestMapping("/requestParam")
public ModelAndView requestParam(@RequestParam("role_name") String roleName, String note) {
System.out.println("進入@RequestParam註解控制器---處理請求端屬性值與接收端參數名稱不一致時。默認情況下該參數不能爲空,爲空時拋出異常");
System.out.println("名稱:" + roleName);
System.out.println("備註:" + note);
ModelAndView mv = new ModelAndView();
mv.addObject("msg", "處理請求端屬性值與接收端參數名稱不一致時。默認情況下該參數不能爲空,爲空時拋出異常");
mv.setViewName("paramsPage.jsp");
return mv;
}
//{id}代表接收一個參數。使用@PathVariable註解表示從URL的請求地址中獲取參數
@RequestMapping("/getRole/{id}")
public ModelAndView pathVariable(@PathVariable("id") int id) {
System.out.println("進入@PathVariable註解控制器---設置視圖爲JSON視圖並返回JSON數據");
System.out.println("從客戶端傳來id:" + id);
Role role = new Role(1,"梅西","Messi");
ModelAndView mv = new ModelAndView();
//綁定數據模型
mv.addObject(role);
//設置爲JSON視圖
mv.setView(new MappingJackson2JsonView());
return mv;
}
//控制器:使用@RequestBody註解接收JSON數據
@RequestMapping(value="/receiveJSON")
public ModelAndView receiveJSON(@RequestBody Role role) {
System.out.println("進入@RequestBody註解控制器");
System.out.println(role.getRoleName() + " " + role.getNote());
//分頁參數
Pagination p = role.getPagination();
System.out.println(p.getStart()+ " " + p.getLimit());
ModelAndView mv = new ModelAndView();
mv.addObject(role);
mv.setView(new MappingJackson2JsonView());
return mv;
}
//控制器:接收數組
@RequestMapping(value="/receiveArray")
public ModelAndView receiveArray(@RequestBody List<Long> list) {
System.out.println("進入RequestBody控制器");
System.out.println(list.get(0));
System.out.println(list.get(1));
System.out.println(list.get(2));
ModelAndView mv = new ModelAndView();
mv.addObject(list);
mv.setView(new MappingJackson2JsonView());
return mv;
}
}