java renderjson用法_Tomcat 下开启ssl之后 renderJson()报错

解决办法是使用: render(new JsonRender(params).forIE());

*/

public class JsonRender extends Render {

private static final long serialVersionUID = 3606364198859021837L;

// 排除转化的属性集合

private static final transient Set excludedAttrs = new HashSet() {

private static final long serialVersionUID = 9186138395157680676L;

{

add("javax.servlet.request.ssl_session");

add("javax.servlet.request.ssl_session_id");

add("javax.servlet.request.ssl_session_mgr");

add("javax.servlet.request.key_size");

add("javax.servlet.request.cipher_suite");

}

};

// 仅对无参 renderJson() 起作用

public static void addExcludedAttrs(String... attrs) {

if (attrs != null)

for (String attr : attrs)

excludedAttrs.add(attr);

}

public static void removeExcludedAttrs(String... attrs) {

if (attrs != null)

for (String attr : attrs)

excludedAttrs.remove(attr);

}

public static void clearExcludedAttrs() {

excludedAttrs.clear();

}

/**

在wiki中查到: 尚未被接受为正式数据类型的subtype,可以使用x-开始的独立名称(例如application/x-gzip)

所以以下可能要改成 application/x-json

通过使用firefox测试,struts2-json-plugin返回的是 application/json, 所以暂不改为 application/x-json

2: IE 不支持 application/json, 在 ajax 上传文件完成后返回 json时 IE 提示下载文件

*/

private static final String contentType = "application/json;charset=" + getEncoding();

private static final String contentTypeForIE = "text/html;charset=" + getEncoding();

private boolean forIE = false;

private static int convertDepth = 8;

public JsonRender forIE() {

forIE = true;

return this;

}

private String jsonText;

private String[] attrs;

public JsonRender() {

}

@SuppressWarnings("serial")

public JsonRender(final String key, final Object value) {

if (key == null)

throw new IllegalArgumentException("The parameter key can not be null.");

this.jsonText = JsonKit.mapToJson(new HashMap(){{put(key, value);}}, convertDepth);

}

public JsonRender(String[] attrs) {

if (attrs == null)

throw new IllegalArgumentException("The parameter attrs can not be null.");

this.attrs = attrs;

}

public JsonRender(String jsonText) {

if (jsonText == null)

throw new IllegalArgumentException("The parameter jsonString can not be null.");

this.jsonText = jsonText;

}

public JsonRender(Object object) {

if (object == null)

throw new IllegalArgumentException("The parameter object can not be null.");

this.jsonText = JsonKit.toJson(object, convertDepth);

}

public static void setConvertDepth(int convertDepth) {

if (convertDepth < 2)

throw new IllegalArgumentException("convert depth can not less than 2.");

JsonRender.convertDepth = convertDepth;

}

public void render() {

if (jsonText == null)

buildJsonText();

PrintWriter writer = null;

try {

response.setHeader("Pragma", "no-cache");// HTTP/1.0 caches might not implement Cache-Control and might only implement Pragma: no-cache

response.setHeader("Cache-Control", "no-cache");

response.setDateHeader("Expires", 0);

response.setContentType(forIE ? contentTypeForIE : contentType);

writer = response.getWriter();

writer.write(jsonText);

writer.flush();

} catch (IOException e) {

throw new RenderException(e);

}

finally {

if (writer != null)

writer.close();

}

}

@SuppressWarnings({"rawtypes", "unchecked"})

private void buildJsonText() {

Map map = new HashMap();

if (attrs != null) {

for (String key : attrs)

map.put(key, request.getAttribute(key));

}

else {

for (Enumeration attrs=request.getAttributeNames(); attrs.hasMoreElements();) {

String key = attrs.nextElement();

if (excludedAttrs.contains(key))

continue;

Object value = request.getAttribute(key);

map.put(key, value);

}

}

this.jsonText = JsonKit.mapToJson(map, convertDepth);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值