jsonConfig用法
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
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));
}
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用法的更多相关文章
- JsonConfig的jsonConfig.setExcludes的用法
1.问题描述 在项目中经常会有两个类存在一对多或者多对一的关联关系,这样在查询多的一方时,会深入查询关联的一方,而我们可能并不需要去深入查询那些数据,此时使用JsonConfig的jsonConfig ...
- json-lib——JsonConfig详细使用说明
在使用json-lib包中JSONObject.fromObject(bean,cfg)时,可能出现以下几种情况: 1.(防止自包含)转换的对象包含自身对象,或者对象A下面挂了对象B,对象B下面又挂了 ...
- JsonConfig处理日期时间
写在前面: 页面发送ajax请求到后台,后台返回对应的json格式数据给前台页面进行数据展示,如果json数据中含有日期时间,就需要对日期进行处理 下面是相关的代码部分 JsonConfig json ...
- EditText 基本用法
title: EditText 基本用法 tags: EditText,编辑框,输入框 --- EditText介绍: EditText 在开发中也是经常用到的控件,也是一个比较必要的组件,可以说它是 ...
- jquery插件的用法之cookie 插件
一.使用cookie 插件 插件官方网站下载地址:http://plugins.jquery.com/cookie/ cookie 插件的用法比较简单,直接粘贴下面代码示例: //生成一个cookie ...
- Java中的Socket的用法
Java中的Socket的用法 Java中的Socket分为普通的Socket和NioSocket. 普通Socket的用法 Java中的 ...
- [转载]C#中MessageBox.Show用法以及VB.NET中MsgBox用法
一.C#中MessageBox.Show用法 MessageBox.Show (String) 显示具有指定文本的消息框. 由 .NET Compact Framework 支持. MessageBo ...
- python enumerate 用法
A new built-in function, enumerate() , will make certain loops a bit clearer. enumerate(thing) , whe ...
- [转载]Jquery中$.get(),$.post(),$.ajax(),$.getJSON()的用法总结
本文对Jquery中$.get(),$.post(),$.ajax(),$.getJSON()的用法进行了详细的总结,需要的朋友可以参考下,希望对大家有所帮助. 详细解读Jquery各Ajax函数: ...
随机推荐
- 读书笔记 - js高级程序设计 - 第五章 引用类型
引用类型 和 类 不是一个概念 用typeof来检测属性是否存在 typeof args.name == "string" 需要实验 访问属性的方法 .号和[] 一般情况下要 ...
- zTree & ckeditor &ValidateCode.jar 使用个人心得总结
zTree:依靠 jQuery 实现的多功能 “树插件”. 使用时只需要将下载的压缩包接用,复制里边的css 和 js 到指定目录即可. 如图所示: 在zTree的官网可以找到各种类型树的示例: 地址 ...
- 2020.2.21一些python总结
#字符串前面加r可以防止字符串转义 \也可以防止字符串转义#三引号可以实现输入多行文本#range(start,end,step)#列表 append添加一个元素到末尾 extend 添加一个素组到末 ...
- 寒假day16
今天优化了管理员界面,人才标签模块遇到了一点问题,部分结果无法显示,正在寻找原因
- Ka/ Ks|同义替换的三种路径|kaks_Calculator|
生命组学 研究old gene 和 young gene CAI选择信号:CGmutation信号 Neutrality plot:CG3与GC1.GC2的关系:平:mutation:正相关:sele ...
- 单细胞测序|单细胞基因组|单细胞转录组|Gene editing|
单细胞测序 单细胞基因组学 测量理由是单细胞的时间空间特异性. Gene expression&co-expression 比较正常cell与疾病cell,正常organ与疾病organ,看出 ...
- ZJNU 2353 - UNO
大模拟,但是题目好像有些地方表述不清 根据UNO在初中曾被别人虐了很久很久的经历 猜测出了原本的题意 本题中的+2虽然有颜色,但是也可以当作原UNO游戏中的+4黑牌 即在某人出了+2后,可以出不同颜色 ...
- JavaScript—面向对象 贪吃蛇_2 食物对象
食物对象 //自调用 (function (){ function Food(element) { this.width = 20 this.height = 20 this.backgroundCo ...
- 吴裕雄--天生自然 JAVA开发学习:变量类型
public class Variable{ static int allClicks=0; // 类变量 String str="hello world"; // 实例变量 pu ...
- log4j2和logback动态修改日志级别工具类
工作中,在排查线上问题时,有以下场景在不重新部署或重启服务的情况下,需要动态调整线上日志级别 1.线上有些日志打印过多干扰有用的日志,需要动态修改线上日志记录器的打印日志级别,调高一些日志级别,打印出 ...