SpringMVC中输出json
2016-04-28 16:57
447 查看
// header 常量定义
private static final String DEFAULT_ENCODING = "GBK";
private static final boolean DEFAULT_NOCACHE = true;
// Content Type 常量定义
public static final String TEXT_TYPE = "text/plain";
public static final String JSON_TYPE = "application/json";
public static final String XML_TYPE = "text/xml";
public static final String HTML_TYPE = "text/html";
public static final String JS_TYPE = "text/javascript";
public static final String EXCEL_TYPE = "application/vnd.ms-excel";
public static int PAGE_NUMERIC = 10;
protected HttpServletResponse initResponseHeader(final String contentType,
final HttpServletResponse response) {
// 分析headers参数
String encoding = DEFAULT_ENCODING;
boolean noCache = DEFAULT_NOCACHE;
// 设置headers参数
String fullContentType = contentType + ";charset=" + encoding;
response.setContentType(fullContentType);
if (noCache) {
setNoCacheHeader(response);
}
return response;
}
/**
* 直接输出内容的简便函数.
*/
protected void render(final String contentType, final String content,
final HttpServletResponse response) {
HttpServletResponse resp = initResponseHeader(contentType, response);
try {
resp.getWriter().write(content);
resp.getWriter().flush();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 输出JSON,可以接收参数如: [{'name':'www'},{'name':'www'}],['a','b'],new
* String[]{'a','b'},合并如下:jsonString = "{TOTALCOUNT:" + totalCount + ",
* ROOT:" + jsonString + "}";
*
* @param jsonString
* json字符串.
*
*/
protected void outJson(final String json, final HttpServletResponse response) {
render(JSON_TYPE, json, response);
}
String json = JSONObject.fromObject(map).toString();
outJson(json, response);
return;
private static final String DEFAULT_ENCODING = "GBK";
private static final boolean DEFAULT_NOCACHE = true;
// Content Type 常量定义
public static final String TEXT_TYPE = "text/plain";
public static final String JSON_TYPE = "application/json";
public static final String XML_TYPE = "text/xml";
public static final String HTML_TYPE = "text/html";
public static final String JS_TYPE = "text/javascript";
public static final String EXCEL_TYPE = "application/vnd.ms-excel";
public static int PAGE_NUMERIC = 10;
protected HttpServletResponse initResponseHeader(final String contentType,
final HttpServletResponse response) {
// 分析headers参数
String encoding = DEFAULT_ENCODING;
boolean noCache = DEFAULT_NOCACHE;
// 设置headers参数
String fullContentType = contentType + ";charset=" + encoding;
response.setContentType(fullContentType);
if (noCache) {
setNoCacheHeader(response);
}
return response;
}
/**
* 直接输出内容的简便函数.
*/
protected void render(final String contentType, final String content,
final HttpServletResponse response) {
HttpServletResponse resp = initResponseHeader(contentType, response);
try {
resp.getWriter().write(content);
resp.getWriter().flush();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 输出JSON,可以接收参数如: [{'name':'www'},{'name':'www'}],['a','b'],new
* String[]{'a','b'},合并如下:jsonString = "{TOTALCOUNT:" + totalCount + ",
* ROOT:" + jsonString + "}";
*
* @param jsonString
* json字符串.
*
*/
protected void outJson(final String json, final HttpServletResponse response) {
render(JSON_TYPE, json, response);
}
String json = JSONObject.fromObject(map).toString();
outJson(json, response);
return;
相关文章推荐
- struts.xml和struts.properties
- spring项目中如何添加定时器以及在定时器中自动生成sprng注入对象
- 关于action默认执行execute()方法一点想法
- Failed to register in JMX: javax.naming.NamingException: Could not load resource factory class
- 对象的访问定位:句柄与直接指针
- eclipse链接hadoop2.5.1时报错Error:Call From roo/10.30.12.xxx to hostname1:9000 failed on connection excep
- [原创] 如何用Eclispe调试java -jar xxx.jar 方式执行的jar包
- Java链表的简单实现
- Eclipse 的单步调试
- Java分布式通信之RMI
- Spring学习笔记--导航
- java.lang.ClassCastException: java.lang.Character cannot be cast to java.lang.String|<s:property val
- java 实现Excel数据导入数据库时,中文乱码问题
- SpringMVC+jade实现高性能模板引擎(简单配置)
- java中命令模式
- 深入浅出学Spring Data JPA
- Java 8怎么了:局部套用vs闭包
- Java 8怎么了:局部套用vs闭包
- cxf3.x +spring 3.x(4.x)+ maven 发布webservice 服务
- spring4.2完整web项目(使用html视图解析器)