前台:
1 DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Titletitle> 6 head> 7 <body> 8 <button id="stringParameter">接收前台String参数button> 9 <button id="integerParameter">接收前台Integer参数button> 10 <button id="bigDecimalParameter">接收前台BigDecimal参数button> 11 <button id="userVoParameter">接收前台对象UserVo参数button> 12 <button id="listUserVoParameter">接收前台List-UserVo-参数button> 13 <button id="listStringParameter">接收前台List-String-参数button> 14 <button id="arrayStringParameter">接收前台String[]参数button> 15 <button id="userVo1Parameter">接收前台UserVo1(UserVo,List-UserVo-,String)参数button> 16 17 <#--引用js--> 18 <script src="/js/jquery-3.1.1.js">script> 19 <script> 20 $("#stringParameter").click(function () { 21 $.ajax({ 22 url:"/stringParameter", 23 type:"POST", 24 data:{name:"传String参数成功"}, 25 success:function (data) { 26 console.log(data); 27 } 28 }) 29 }); 30 $("#integerParameter").click(function () { 31 $.ajax({ 32 url:"/integerParameter", 33 type:"POST", 34 data:{count:100000}, 35 success:function (data) { 36 console.log(data); 37 } 38 }) 39 }); 40 $("#bigDecimalParameter").click(function () { 41 $.ajax({ 42 url:"/bigDecimalParameter", 43 type:"POST", 44 data:{money:10000.345435}, 45 success:function (data) { 46 console.log(data); 47 } 48 }) 49 }); 50 $("#userVoParameter").click(function () { 51 $.ajax({ 52 url:"/userVoParameter", 53 type:"POST", 54 data:{ 55 name:"名字", 56 password:"12345678", 57 age:500, 58 money:1314.520 59 }, 60 success:function (data) { 61 console.log(data); 62 } 63 }) 64 }); 65 $("#listUserVoParameter").click(function () { 66 var userVos = new Array(); 67 var userVo1 ={ 68 name:"名字1", 69 password:"12345678", 70 age:500, 71 money:1314.520 72 }; 73 var userVo2 ={ 74 name:"名字2", 75 password:"12345678", 76 age:500, 77 money:1314.520 78 }; 79 userVos.push(userVo1); 80 userVos.push(userVo2); 81 $.ajax({ 82 url:"/listUserVoParameter", 83 type:"POST", 84 data:{userVos:JSON.stringify(userVos)},//将数组转化成json,然后后台用字符串接受再转化格式 85 success:function (data) { 86 console.log(data); 87 } 88 }) 89 }); 90 $("#listStringParameter").click(function () { 91 var names = new Array(); 92 names.push("名字1"); 93 names.push("名字2"); 94 names.push("名字3"); 95 names.push("名字4"); 96 names.push("名字5"); 97 names.push("名字6"); 98 $.ajax({ 99 url:"/listStringParameter", 100 type:"POST", 101 data:{names:names}, 102 success:function (data) { 103 console.log(data); 104 } 105 }) 106 }); 107 $("#arrayStringParameter").click(function () { 108 var nameArray = new Array(); 109 nameArray.push("名字1"); 110 nameArray.push("名字2"); 111 nameArray.push("名字3"); 112 nameArray.push("名字4"); 113 nameArray.push("名字5"); 114 nameArray.push("名字6"); 115 $.ajax({ 116 url:"/arrayStringParameter", 117 type:"POST", 118 data:{nameArray:nameArray}, 119 success:function (data) { 120 console.log(data); 121 } 122 }) 123 }); 124 $("#userVo1Parameter").click(function () { 125 //对象 126 var userVo ={ 127 name:"名字1", 128 password:"12345678", 129 age:500, 130 money:1314.520 131 }; 132 133 //list对象 134 var userVos = new Array(); 135 var userVo1 ={ 136 name:"名字1", 137 password:"12345678", 138 age:500, 139 money:1314.520 140 }; 141 var userVo2 ={ 142 name:"名字2", 143 password:"12345678", 144 age:500, 145 money:1314.520 146 }; 147 userVos.push(userVo1); 148 userVos.push(userVo2); 149 150 //String 151 var name ="名字"; 152 var userVo1 = { 153 userVo:userVo, 154 userVos:userVos, 155 name:name 156 } 157 $.ajax({ 158 url:"/userVo1Parameter", 159 type:"POST", 160 data:JSON.stringify(userVo1), 161 contentType: 'application/json;charset=utf-8', 162 success:function (data) { 163 console.log(data); 164 } 165 }) 166 }); 167 script> 168 body> 169 html>
后台:
1 package com.parameter.controller; 2 3 import com.alibaba.fastjson.JSON; 4 import com.parameter.vo.UserVo; 5 import com.parameter.vo.UserVo1; 6 import lombok.extern.slf4j.Slf4j; 7 import org.springframework.stereotype.Controller; 8 import org.springframework.web.bind.annotation.RequestBody; 9 import org.springframework.web.bind.annotation.RequestMapping; 10 import org.springframework.web.bind.annotation.RequestParam; 11 import org.springframework.web.bind.annotation.ResponseBody; 12 13 import java.math.BigDecimal; 14 import java.util.List; 15 16 @Controller 17 @Slf4j 18 public class ParameterController { 19 20 @RequestMapping("/index") 21 public String index() { 22 return "/index/index"; 23 } 24 25 /** 26 * 接收前台String参数 27 * 28 * @param name 29 * @return 30 */ 31 @RequestMapping("/stringParameter") 32 @ResponseBody 33 public String stringParameter(String name) { 34 log.info("stringParameter:{}", name); 35 return name; 36 } 37 38 /** 39 * 接收前台Integer参数 40 * 41 * @param count 42 * @return 43 */ 44 @RequestMapping("/integerParameter") 45 @ResponseBody 46 public Integer integerParameter(Integer count) { 47 log.info("integerParameter:{}", count); 48 return count; 49 } 50 51 /** 52 * 接收前台BigDecimal参数 53 * 54 * @param money 55 * @return 56 */ 57 @RequestMapping("/bigDecimalParameter") 58 @ResponseBody 59 public BigDecimal bigDecimalParameter(BigDecimal money) { 60 log.info("bigDecimalParameter:{}", money); 61 return money; 62 } 63 64 /** 65 * 接收前台对象UserVo参数 66 * 67 * @param userVo 68 * @return 69 */ 70 @RequestMapping("/userVoParameter") 71 @ResponseBody 72 public UserVo userVoParameter(UserVo userVo) { 73 log.info("userVoParameter:{}", JSON.toJSONString(userVo)); 74 return userVo; 75 } 76 77 /** 78 * 接收前台List参数 79 * 1.首先修改ajax传递参数的方式,JSON.stringify(perArr); 将数组转化成json,然后后台用字符串接受再转化格式JSON.parseArray(userVos, UserVo.class); 80 * 2.将List封装在一个对象里面,在包一层 81 * 82 * @param userVos 83 * @return 84 */ 85 @RequestMapping("/listUserVoParameter") 86 @ResponseBody 87 public ListlistUserVoParameter(String userVos) { 88 List userVoList = JSON.parseArray(userVos, UserVo.class); 89 log.info("listUserVoParameter:{}", JSON.toJSONString(userVoList)); 90 return userVoList; 91 } 92 93 /** 94 * 接收前台List 参数 95 * 96 * @param names 97 * @return 98 */ 99 @RequestMapping("/listStringParameter") 100 @ResponseBody 101 public ListlistStringParameter(@RequestParam("names[]") List names) { 102 log.info("listStringParameter:{}", JSON.toJSONString(names)); 103 return names; 104 } 105 106 /** 107 * 接收前台String[]参数 108 * 109 * @param names 110 * @return 111 */ 112 @RequestMapping("/arrayStringParameter") 113 @ResponseBody 114 public String[] arrayStringParameter(@RequestParam("nameArray[]") String[] nameArray) { 115 log.info("arrayStringParameter:{}", JSON.toJSONString(nameArray)); 116 return nameArray; 117 } 118 119 /** 120 * 接收前台UserVo1参数 121 * 122 * @param names 123 * @return 124 */ 125 @RequestMapping("/userVo1Parameter") 126 @ResponseBody 127 public UserVo1 userVo1Parameter(@RequestBody UserVo1 userVo1) { 128 log.info("userVo1Parameter:{}", JSON.toJSONString(userVo1)); 129 return userVo1; 130 } 131 }
UserVo.class
1 package com.parameter.vo; 2 3 import lombok.Data; 4 5 import java.io.Serializable; 6 import java.math.BigDecimal; 7 8 @Data 9 public class UserVo implements Serializable { 10 11 private String name; 12 13 private String password; 14 15 private Integer age; 16 17 private BigDecimal money; 18 }
UserVo1.class
1 package com.parameter.vo; 2 3 import lombok.Data; 4 5 import java.io.Serializable; 6 import java.util.List; 7 8 @Data 9 public class UserVo1 implements Serializable { 10 11 UserVo userVo; 12 13 ListuserVos; 14 15 String name; 16 }