SpringMVC—对Ajax的处理(含 JSON 类型)(3)
五、服务器端的 SpringMVC 如何返回 JSON 类型的字符串。
请求:
$("#testJson8").click(function () {
$.ajax({
url: "testReturnJsonValue",
type: "post",
success: function (result) {
console.log(result);
}
});
});
1.返回单个对象
handler 方法:
@ResponseBody
@RequestMapping("/testReturnJsonValue")public Person testReturnJsonValue() {
Person person = new Person();
person.setName("lily");
person.setAge(23); return person;
}
在浏览器控制台正常打印了 Person 对象。
注意:这里没有指定 dataType。
2.返回多个对象
handler 方法:
@ResponseBody
@RequestMapping("/testReturnJsonValue")public List<Person> testReturnJsonValue() {
List<Person> personList = new ArrayList<>(); Person person = new Person();
person.setName("lily");
person.setAge(23);
Person person2 = new Person();
person2.setName("lucy");
person2.setAge(33);
personList.add(person);
personList.add(person2); return personList;
}
在浏览器控制条正常打印了 Person 数组。
3.返回 Map
@ResponseBody
@RequestMapping("/testReturnJsonValue")public Map<String, Person> testReturnJsonValue() {
Map<String, Person> map = new HashMap<>(); Person person = new Person();
person.setName("lily");
person.setAge(23);
Person person2 = new Person();
person2.setName("lucy");
person2.setAge(33); map.put("1", person);
map.put("2", person2); return map;
}
浏览器控制台输出:
4.在实际生产环境下的 Ajax 返回值。
封装一个返回值类型:
public class AjaxResult implements Serializable {
public static final String RESULT_CODE_0000 = "0000";
public static final String RESULT_CODE_0001 = "0001";
private String code;
private String message;
private Object data;
public AjaxResult() {
}
public String getCode() { return this.code;
}
public void setCode(String code) { this.code = code;
}
public String getMessage() { return this.message;
}
public void setMessage(String message) { this.message = message;
}
public Object getData() { return this.data;
}
public void setData(Object data) { this.data = data;
}
}
实际使用:
@ResponseBody
@RequestMapping("/testReturnJsonValue")
public AjaxResult testReturnJsonValue() {
AjaxResult ajaxResult = new AjaxResult();
try {
Map<String, Person> map = new HashMap<>(); Person person = new Person();
person.setName("lily");
person.setAge(23);
Person person2 = new Person();
person2.setName("lucy");
person2.setAge(33); map.put("1", person);
map.put("2", person2);
ajaxResult.setData(map);
ajaxResult.setMessage("success!");
ajaxResult.setCode(AjaxResult.RESULT_CODE_0000);
} catch(Exception e) {
e.printStackTrace();
ajaxResult.setMessage("fail!");
ajaxResult.setCode(AjaxResult.RESULT_CODE_0001);
} return ajaxResult;
}
六、Request Payload
(1)出现的条件:
contentType: 'application/json;charset=utf-8'
type:post
(2)具体请参看
http://xiaobaoqiu.github.io/blog/2014/09/04/form-data-vs-request-payload/
(3)建议尽量不要手动的去处理此种情况,能选用别的方式避免就尽量避免。
七、总结
主要介绍了SpringMVC 对 Ajax 的支持,对与 Ajax 数据如何组织,重点介绍了对表单的支持。
SpringMVC—对Ajax的处理(含 JSON 类型)(3)的更多相关文章
- SpringMVC—对Ajax的处理(含 JSON 类型)(2)
这里编写了一个通用的类型转换器: 用来转换形如: firstName=jack&lastName=lily&gender=1&foods=Steak&foods=Piz ...
- SpringMVC—对Ajax的处理(含 JSON 类型)(1)
一.首先要搞明白的一些事情. 1.从客户端来看,需要搞明白: (1)要发送什么样格式的 JSON 数据才能被服务器端的 SpringMVC 很便捷的处理,怎么才能让我们写更少的代码,如何做好 JSON ...
- 本文使用springMVC和ajax,实现将JSON对象返回到页面
一.引言 本文使用springMVC和ajax做的一个小小的demo,实现将JSON对象返回到页面,没有什么技术含量,纯粹是因为最近项目中引入了springMVC框架. 二.入门例子 ①. 建立工程, ...
- 通过Ajax进行POST提交JSON类型的数据到SpringMVC Controller的方法
现在在做的项目用到了SpringMVC框架,需要从前端angular接收请求的JSON数据,为了测试方便,所以直接先用AJAX进行测试,不过刚开始用平时用的ajax方法,提交请求会出现415或者400 ...
- springmvc通过ajax异步请求返回json格式数据
jsp 首先创建index.jsp页面 <script type="text/javascript"> $(function () { $("#usernam ...
- SpringMVC——对Ajax的处理(包含 JSON 类型)
一.首先要搞明白的一些事情. 1.从客户端来看,需要搞明白: (1)要发送什么样格式的 JSON 数据才能被服务器端的 SpringMVC 很便捷的处理,怎么才能让我们写更少的代码,如何做好 JSON ...
- SpringMVC中出现" 400 Bad Request "错误(用@ResponseBody处理ajax传过来的json数据转成bean)的解决方法
最近angularjs post到后台 400一头雾水 没有任何错误. 最后发现好文,感谢作者 SpringMVC中出现" 400 Bad Request "错误(用@Respon ...
- springMVC参数绑定JSON类型的数据
需求就是: 现在保存一个Student,并且保存Student的friend,一个student会有多个朋友,这里要传递到后台的参数是: var friends = new Array(); var ...
- springmvc接收JSON类型的数据
1.在使用AJAX传递JSON数据的时候要将contentType的类型设置为"application/json",否则的话会提示415错误 2.传递的data需要时JSON类型的 ...
随机推荐
- JavaScript实现的功能
1.js事件阻止冒泡的应用 1)问题描述: 单机除了这两个元素,触发事件,: 可用阻止事件冒泡 2)解决方法: $('body').click(function(e){ $('#searchTree' ...
- RowSet
import java.io.FileInputStream; import java.util.Properties; import javax.sql.rowset.JdbcRowSet; imp ...
- 机器学习之K-近邻(KNN)算法
一 . K-近邻算法(KNN)概述 最简单最初级的分类器是将全部的训练数据所对应的类别都记录下来,当测试对象的属性和某个训练对象的属性完全匹配时,便可以对其进行分类.但是怎么可能所有测试对象都会找到 ...
- STL视频_01
ZC:这里视频里面有一个调试小技巧,VS08/VS2010开始,控制台程序会自动退出(不像VC6),那么可以在 函数退出的最后一句语句上设置断点,然后查看控制台打印出来的信息.ZC:这一讲,给我的感觉 ...
- Windows 运行中的命令
辅助功能选项 access.cpl 添加硬件向导 hdwwiz.cpl 添加或删除程序 appwiz.cpl 管理工具 control admintools 自动更新 wuaucpl.cpl Blue ...
- leetcode 645. Set Mismatch——凡是要节约空间的题目 都在输入数据上下功夫 不要担心破坏原始的input
The set S originally contains numbers from 1 to n. But unfortunately, due to the data error, one of ...
- Java_数据交换_JAXB_用法入门
一.前言 最近有个需求,需要进行xml 与 bean 的相互转化. 使用 JAXB 可完成这个需求. 二.概述 JAXB(Java Architecture for XML Binding) 是一个业 ...
- python3中zip()函数的用法
>>>a = [1,2,3] >>> b = [4,5,6] >>> c = [4,5,6,7,8] >>> zipped = ...
- javascript网页复制功能-复制到粘贴板-兼容多数浏览器(不使用flash)
使用方法:clipBordCopy("hello Copy");//执行后复制hello Copy到粘贴板 通过 var result = clipBordCopy("h ...
- scrapy入门实践1
Scrapy是一个为了爬取网站数据,提取结构性数据而编写的应用框架. 可以应用在包括数据挖掘,信息处理或存储历史数据等一系列的程序中. 这就是整个Scrapy的架构图了: 各部件职能: Scrapy ...