Response返回JSON数据到前台页面
转自博文:《Response JSON数据返回》http://blog.csdn.net/anialy/article/details/8665471
简述:
在servlet填充Response的时候,做JSON格式的数据转换
使用的类是net.sf.json.JSONObject,传入response对象和返回的显示类,修改response,返回前台JSON格式数据
代码:
- /**
- * 以JSON格式输出
- * @param response
- */
- protected void responseOutWithJson(HttpServletResponse response,
- Object responseObject) {
- //将实体对象转换为JSON Object转换
- JSONObject responseJSONObject = JSONObject.fromObject(responseObject);
- response.setCharacterEncoding("UTF-8");
- response.setContentType("application/json; charset=utf-8");
- PrintWriter out = null;
- try {
- out = response.getWriter();
- out.append(responseJSONObject.toString());
- logger.debug("返回是\n");
- logger.debug(responseJSONObject.toString());
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- if (out != null) {
- out.close();
- }
- }
- }
例如:
try {
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
String selectName = new String(request.getParameter("selectName").getBytes("iso-8859-1"),"utf-8");//用request获取URL传递的中文参数,防止乱码
response.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
if (!selectName.equals("")) {
historyEvent = historyEventService.getHistoryEventByName(projectId, selectName);//获取对象
response.setContentType("application/json; charset=utf-8");
JSONObject responseJSONObject = JSONObject.fromObject(historyEvent); //将实体对象转换为JSON Object转换
out.print(responseJSONObject.toString());
out.flush();
out.close();
}
} catch (Exception e) {
e.printStackTrace();
}
Response返回JSON数据到前台页面的更多相关文章
- 通过flask中的Response返回json数据
使用flask的过程中,发现有时需要生成一个Response并返回.网上查了查,看了看源码,找到了两种办法: from flask import Response, json Response(jso ...
- springmvc 返回json数据给前台jsp页面展示
spring mvc返回json字符串的方式 方案一:使用@ResponseBody 注解返回响应体 直接将返回值序列化json 优点:不需要自己再处理 步骤一:在spring- ...
- 后台返回json数据,前台显示代码
List list = "从DAL获取的数据集合" //取出分页标签html int pageIndex = context.Request["pageIndex&quo ...
- 用ajax获取后台数据,返回json数据,怎么在前台使用?
用ajax获取后台数据,返回json数据,怎么在前台使用呢?后台 C# code ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 if (dataType == &qu ...
- Ajax前台返回JSON数据后再Controller中直接转换成类型使用,后台接收json转成实体的方法
之前写过一篇记录文章,写的是将一个比较复杂的数据结构在前台组合起来后传递到后台. 当时并不太了解@RequestBody,也并没有使用js提供的JSON.stringify()方法 所有都是自己写的, ...
- Flask Response响应(flask中设置响应信息的方法,返回json数据的方法)
设置响应信息的方法 1. 返回自定义的响应头,有两种方式: (1) 第一种是:视图函数return的时候,使用元组,返回自定义的信息 返回的时候的状态码可以自定义信息:"状态码 自定 ...
- Atitit.列表页面and条件查询的实现最佳实践(1)------设置查询条件and提交查询and返回json数据
Atitit.列表页面and条件查询的实现最佳实践(1)------设置查询条件and提交查询and返回json数据 1. 1. 配置条件字段@Conditional 1 1 2. 2. 配置条件字段 ...
- 前台返回json数据的常用方式+常用的AJAX请求后台数据方式
我个人开发常用的如下所示: 之所以像下面这样下,一是前台Ajax,二是为安卓提供接口数据 现在常用的是返回JSON数据,XML的时代一去不复返 JSON相对于XML要轻量级的多 对JSON不是十分熟悉 ...
- 用easyui从servlet传递json数据到前端页面的两种方法
用easyui从servlet传递json数据到前端页面的两种方法 两种方法获取的数据在servlet层传递的方法相同,下面为Servlet中代码,以查询表中所有信息为例. //重写doGet方法 p ...
随机推荐
- Highlighting Text Item On Entry In Oracle Forms
Highlight a Text Item in Oracle Forms With Visual Attribute It is very necessary to highlight the cu ...
- 4、Type fundamentals
1.All Types Are Derived from System.Object The CLR requires all objects to be created using the new ...
- [数据结构与算法]哈夫曼(Huffman)树与哈夫曼编码
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- [SAP ABAP开发技术总结]反射,动态创建内表、结构、变量
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- POJ 3069 Saruman's Army(萨鲁曼军)
POJ 3069 Saruman's Army(萨鲁曼军) Time Limit: 1000MS Memory Limit: 65536K [Description] [题目描述] Saruman ...
- python tools: iPython Notebook
Introducing IPython Notebook IPython isn't a different programming language, it's just a set of comp ...
- QQ音乐项目(OC版) - 实现细节
QQ 音乐看似简单,但自己手动实现起来,才发现没有那么简单,有好多细节,需要注意. github : https://github.com/keenleung/QQMusic-OC 一.业务逻辑 首先 ...
- webview页面和壳通信的库(精简版)
// PG精简版 (function() { var PG = { iosBridge: null, callbackId: 0, callbacks: [], commandQueue: [], c ...
- 2014 Multi-University Training Contest 4
1006 hdu4902 #include <iostream> #include<stdio.h> #include<vector> #include<qu ...
- C++——将成员函数作为参数
在C++中,成员函数指针作为参数传递给其他函数和普通函数指针的传递是不同的,首先 我们来回顾一下普通函数指针的传递方法: //------------------------------------- ...