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. JsonConfig的jsonConfig.setExcludes的用法

    1.问题描述 在项目中经常会有两个类存在一对多或者多对一的关联关系,这样在查询多的一方时,会深入查询关联的一方,而我们可能并不需要去深入查询那些数据,此时使用JsonConfig的jsonConfig ...

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

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

  3. JsonConfig处理日期时间

    写在前面: 页面发送ajax请求到后台,后台返回对应的json格式数据给前台页面进行数据展示,如果json数据中含有日期时间,就需要对日期进行处理 下面是相关的代码部分 JsonConfig json ...

  4. EditText 基本用法

    title: EditText 基本用法 tags: EditText,编辑框,输入框 --- EditText介绍: EditText 在开发中也是经常用到的控件,也是一个比较必要的组件,可以说它是 ...

  5. jquery插件的用法之cookie 插件

    一.使用cookie 插件 插件官方网站下载地址:http://plugins.jquery.com/cookie/ cookie 插件的用法比较简单,直接粘贴下面代码示例: //生成一个cookie ...

  6. Java中的Socket的用法

                                   Java中的Socket的用法 Java中的Socket分为普通的Socket和NioSocket. 普通Socket的用法 Java中的 ...

  7. [转载]C#中MessageBox.Show用法以及VB.NET中MsgBox用法

    一.C#中MessageBox.Show用法 MessageBox.Show (String) 显示具有指定文本的消息框. 由 .NET Compact Framework 支持. MessageBo ...

  8. python enumerate 用法

    A new built-in function, enumerate() , will make certain loops a bit clearer. enumerate(thing) , whe ...

  9. [转载]Jquery中$.get(),$.post(),$.ajax(),$.getJSON()的用法总结

    本文对Jquery中$.get(),$.post(),$.ajax(),$.getJSON()的用法进行了详细的总结,需要的朋友可以参考下,希望对大家有所帮助. 详细解读Jquery各Ajax函数: ...

随机推荐

  1. 新iPhone上市会让富士康迎来新一轮的“用工荒”吗?

    新iPhone上市,在接下来的几个月内,中国制造会迎来一段非常忙碌或者说忙乱的日子,之所以说忙碌,是在于苹果的订单量常常大得吓人,而之所以说忙乱,则在于连同富士康.和硕.绿点科技等制造业巨头都可能遭遇 ...

  2. C语言的字符串类型

    C语言使用指针来管理字符串(1)C语言中定义字符串的方法:char *p = "linux";此时p就叫字符串,但是实际上p只是一个字符指针(本质上就是一个指针变量,只是p指向了一 ...

  3. nginx 4层代理配置

    1.nginx 从1.9.0版本开始支持四层代理,但做四层代理时 编译需要添加  --with-stream模块 # ./configure --prefix=/usr/local/nginx--us ...

  4. JS-语句二

    for循环的4个要素: 1.初始值        2.条件判断        3.状态改变        4.循环体 for循环的写法: for(var i=0;i>10;i++)        ...

  5. rabbitmq安装及简单demo练习

    参考:https://my.oschina.net/loveorange/blog/3026473 安装参考链接: 1. 下载自己需要的rabbitmq_server(http://www.rabbi ...

  6. 【Python】关于import QtCore报错的处理方法

    刚开始学习使用PyQT,但总碰到一些小挫折 比如 import Pyqt成功 而 from PyQt5 import QtCore, QtGui, QtWidgets却报错,找了半天终于找到资料,原因 ...

  7. retrofit 上传文件 跟参数

    @Multipart @POST("postFied") Call<Void> postFied(@PartMap Map<String,String> m ...

  8. oi笔记——抽象的深度优先搜索

    oi笔记--抽象的深度优先搜索 例题: \(N个数中选K个数,选出的和要为sum\) 例题分析: 对于每个点,我们可以按"选"和"不选"进行搜索,如图: 或者0 ...

  9. 测试Java程序执行耗费的时间

    package test; public class Main { public static void main(String[] args) { long start = System.curre ...

  10. 阿里云Linux格式化数据盘,分区并挂载一个文件系统

    阿里云一块全新的数据盘挂载到ECS实例后,您必须创建并挂载至少一个文件系统.本示例使用I/O优化实例,操作系统为CentOS 7.6,为一块新的300GiB数据盘(设备名为/dev/vdb)创建一个M ...