1.先编写jsonConfig的初始化代码

private JsonConfig
jsonConfig;

public action构造方法() {

jsonConfig = new JsonConfig();

jsonConfig.registerJsonValueProcessor(Date.class,
newJsonValueProcessor() {

private SimpleDateFormat
sdf = new SimpleDateFormat("yyyy-MM-ddHH:mm:ss");

@Override

public Object processObjectValue(String key,Object value, JsonConfig jsonConfig) {

return
this
.process(value);

}

@Override

public Object processArrayValue(Object value,JsonConfig arg1) {

return
this
.process(value);

}

// 处理Date类型返回的Json数值

private Object process(Object value) {

if (value ==
null) {

return
"";

} else
if
(value instanceof Date)

return
sdf.format((Date)value);

else {

return value.toString();

}

}

});

// 不该传给前台的字段

jsonConfig.setJsonPropertyFilter(new PropertyFilter() {

public
boolean
apply(Objectsource, String name, Object value) {

if (source
instanceof RaffleLog) {

if ("contact".equals(name)) {

return
true
;

}

}

return
false
;

}

});

}

/**

* @param response

* @param returnType

* @throws IOException

*/

private
void
printReturnType2Response(HttpServletResponse response, ReturnType<?>returnType)
throws IOException {

response.setCharacterEncoding("UTF-8");

response.setContentType("application/json;charset=utf-8");

response.getWriter().print(JSONObject.fromObject(returnType,
jsonConfig));

}

2.再在须要跳转的action方法中编写对应的返回代码

ReturnType<Map<String,Integer>> returnType = newReturnType<Map<String, Integer>>();

if(条件){

returnType.setStatus(ReturnType.Status.SUCCESS.getValue());

}else{

returnType.setStatus(ReturnType.Status.FAIL.getValue());

}

returnType.setMsg(sb.toString());

this.printReturnType2Response(response,returnType);

3.一个案例分析:

/**
*
* @Title:DocInfoCustomAction.java
* @Copyright:Copyright (c) 2005
* @Description:<br>
* @Created on 2014-4-16 上午9:22:25
* @author 杨凯
*/
public class DocInfoCustomAction extends DispatchAction{ private finalInteger pageSize = 15; // 每页显示页数 Logger logger= Logger.getLogger(DocInfoCustomAction.class); privateJsonConfig jsonConfig; publicDocInfoCustomAction() { jsonConfig = new JsonConfig();
jsonConfig.registerJsonValueProcessor(Date.class,new JsonValueProcessor() {
private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-ddHH:mm:ss"); @Override
public Object processObjectValue(String key, Object value, JsonConfigjsonConfig) {
return this.process(value);
} @Override
public Object processArrayValue(Object value, JsonConfig arg1) {
return this.process(value);
} // 处理Date类型返回的Json数值
private Object process(Object value) {
if (value == null) {
return "";
}else if (value instanceof Date)
return sdf.format((Date) value);
else {
return value.toString();
}
}
});
// 不该传给前台的字段
jsonConfig.setJsonPropertyFilter(new PropertyFilter() {
public boolean apply(Object source, String name, Object value) {
if (source instanceof RaffleLog) {
if ("contact".equals(name)) {
return true;
}
}
return false;
}
});
} /**
* @paramresponse
* @paramreturnType
* @throwsIOException
*/
private voidprintReturnType2Response(HttpServletResponse response, ReturnType<?>returnType) throws IOException {
response.setCharacterEncoding("UTF-8");
response.setContentType("application/json;charset=utf-8");
response.getWriter().print(JSONObject.fromObject(returnType,jsonConfig));
}
/**
* 批量删除操作
*
* @parammapping
* @paramform
* @paramrequest
* @paramresponse
* @return
* @throwsIOException
* @throwsAppException
*/
publicActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequestrequest, HttpServletResponse response) throws IOException, AppException { Stringids = Tool.getDefaultValue(request, "ids", ""); // 获取下拉列表的值
StringBuffer sb = new StringBuffer();
ReturnType<Map<String, Integer>> returnType = newReturnType<Map<String, Integer>>(); if (ids!= null && !("").equals(ids)) {
try {
String[] idds = ids.split(",");
for (String id : idds) {
int flag = DocInfoTempletApi.deleteDocInfoCustom(id);
sb.append("ID为:" + id);
if (flag == 1) {
sb.append(" 的记录删除成功!");
} else {
sb.append(" 的记录删除失败!");
}
sb.append("\r\n");
}
}catch (Exception e) {
logger.debug("DocInfoCustomAction.delete():" + e);
}
returnType.setStatus(ReturnType.Status.SUCCESS.getValue());
} else {
sb.append("条件id不能为空");
returnType.setStatus(ReturnType.Status.FAIL.getValue());
}
returnType.setMsg(sb.toString());
this.printReturnType2Response(response, returnType);
returnnull;
}
}

jsonConfig使用方法的更多相关文章

  1. JAVA对象转化JSON出现死循环问题

    主要是解决JSON因Hibernate映射生成的集合的转化出现的死循环问题. 这个方法很重要 public String ajaxJsonByObjectDirecdt(Object obj, Str ...

  2. Android学习笔记之Json的使用....

    PS:当你的能力还驾驭不了你的目标时,那你需要沉下心来历练... 学习内容: 1.Json的使用... 2.Json信息过滤... 3.从网络上获取Json数据... 4.解析Json数据获取各个属性 ...

  3. JAVA利用反射映射JSON对象为JavaBean

    关于将JSONObject转换为JavaBean,其实在JSONObject中有对于的toBean()方法来处理,还可以根据给定的JsonConfig来处理一些相应的要求,比如过滤指定的属性 //返回 ...

  4. javaSE27天复习总结

    JAVA学习总结    2 第一天    2 1:计算机概述(了解)    2 (1)计算机    2 (2)计算机硬件    2 (3)计算机软件    2 (4)软件开发(理解)    2 (5) ...

  5. 使用JsonConfig中的setExcludes方法过滤不需要转换的属性

    Hibernate的many-to-one双向关联中,查询many方时会将one方数据顺带着查询,同时one中会有List<Many>,然后又会去查Many中的数据... 周而复始,结果j ...

  6. json-lib——JsonConfig详细使用说明

    在使用json-lib包中JSONObject.fromObject(bean,cfg)时,可能出现以下几种情况: 1.(防止自包含)转换的对象包含自身对象,或者对象A下面挂了对象B,对象B下面又挂了 ...

  7. 关于Web项目里的给表单验证控件添加结束时间不得小于开始时间的验证方法,日期转换和前台显示格式之间,还有JSON取日期数据格式转换成标准日期格式的问题

    项目里有些不同页面间的日期显示格式是不同的, 第一个问题: 比如我用日期控件WdatePicker.js导包后只需在input标签里加上onClick="WdatePicker()" ...

  8. json学习系列(2)-生成JSONObject的方法

    生成JSONObject一般有两种方式,通过javabean或者map类型来生成.如下面的例子: 先定义一个User实体类: package com.pcitc.json; /** * 用户实体类 * ...

  9. Spring+Mybatis+jQuery.Pagination.js异步分页及JsonConfig的使用

    在开发工作中经常用到异步分页,这里简单整理一下资料. 一.Controller方法 package com.lwj.controller; import javax.servlet.http.Http ...

随机推荐

  1. 【58沈剑架构系列】mysql并行复制优化思路

    一.缘起 mysql主从复制,读写分离是互联网用的非常多的mysql架构,主从复制最令人诟病的地方就是,在数据量较大并发量较大的场景下,主从延时会比较严重. 为什么mysql主从延时这么大? 回答:从 ...

  2. C语言:1孩半问题

    题目: 一孩半,又称独女户二胎,即中国大陆部分农村的一项计划生育政策,第一胎是女孩的夫妻可以生育第二个子女.如果第二胎有n%人工性别选择干预(选择男孩),试问男女比例为多少.(10分)题目内容: 一孩 ...

  3. 【BZOJ】1152: [CTSC2006]歌唱王国Singleland

    题解 读错题了,是最后留下一个牛人首长歌颂他,和其他人没有关系,t就相当于数据组数 结论题,具体可看 https://www.zhihu.com/question/59895916/answer/19 ...

  4. PLSQL Developer个性化设置

    1)代码自动完成 和讨厌的.才后出现提示说88,我用快捷键任意呼唤. Tools->Preferences->User Interface->Key Configuration.找到 ...

  5. 本机ip和127.0.0.1的区别

    简介 连接上因特网的每一台计算机都会有一个IP地址, 在linux下可以使用命令ifconfig来查看本机的ip地址(windows为ipconfig), 比如我当前电脑的ip地址为192.168.0 ...

  6. Ionic Js五:单选框操作

    实例中,根据选中的不同选项,显示不同的值. HTML 代码 <ion-header-bar class="bar-positive"> <h1 class=&qu ...

  7. forkcms 开启调试模式

    You can enable debug mode by adding "SetEnv FORK_DEBUG 1" in your virtualhosts file.

  8. Scala入门3(特质线性化)

    尝试设计一套特质,灵活的改动整数队列.队列有两种操作:put把整数放入队列,get从尾部取出它们.队列是先进先出的,get应该依照入队列的顺序取数据.提示:可以用mutable.ArrayBuffer ...

  9. react篇章-React State(状态)-组件都是真正隔离的

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <title&g ...

  10. Java_常用API

    1.基本类型包装类 1.1 概述 在实际程序使用中,程序界面上用户输入的数据都是以字符串类型进行存储的.而程序开发中,我们需要把字符串数据,根据需求转换成指定的基本数据类型,如年龄需要转换成int类型 ...