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. 18 12 2 数据库 sql 的增删改查

    ---恢复内容开始--- 1  开始进入MySQL 的安装  https://www.cnblogs.com/ayyl/p/5978418.html  膜拜大神的博客 2  默认安装的时候     m ...

  2. k8认证机制

    参考下面博文 http://www.mamicode.com/info-detail-2270627.html 需要补充: k8s的的认证机制场景使用 客户端证书认证         采用双向证书进行 ...

  3. VC6.0 The value of ESP was not properly saved across a function call 错误解决方法

    调用DLL函数,出现错误 Run-Time Check Failure #0 - The value of ESP was not properly saved across a function c ...

  4. “帮你”校园资讯平台app使用体验

    该app由我的17级学长学姐编写而成,主要功能失物招领,二手市场,表白墙.该软件目前只是面向本校的各专业学生,为内测版本.该软件的注册流程简单,只需要学号确定身份后即可登陆,并且发布各种信息,或者与丢 ...

  5. Spring原理系列一:Spring Bean的生命周期

    一.前言 在日常开发中,spring极大地简化了我们日常的开发工作.spring为我们管理好bean, 我们拿来就用.但是我们不应该只停留在使用层面,深究spring内部的原理,才能在使用时融汇贯通. ...

  6. UML-领域模型的精化

    拙劣的分类和错误的概括是混乱生活的祸根.--H.G.Wells的总结 1.is-a原则 子类定义的成员变量.方法与超类必须一致.即:不能多,也不能少. 子类是“一种”超类.CreditPayment是 ...

  7. Redis的数据结构和对象。

    一.简单动态字符串(simple dynamic string--SDS) Redis使用SDS表示字符串值,键值对都用SDS实现.SDS中的字符数组buf以空字符串结尾,好处是可以直接重用一部分C字 ...

  8. 异常依然执行{try..catch语句块..}的后续代码

    测试异常依然执行{try..catch语句块..}的后续代码: private static Integer testThrows() throws Exception{ Integer result ...

  9. 转:以下是目前已经建立的sub一览 来自:https://zhuanlan.zhihu.com/p/91935757

    转:以下是目前已经建立的sub一览  来自:https://zhuanlan.zhihu.com/p/91935757 作者: Lorgar 理工科 科学(和英文r/science一样,只接受论文讨论 ...

  10. 黑马oracle_day02:04.oracle对象&&05.oracle编程(a)

    01.oracle体系结构 02.oracle的基本操作 03.oracle的查询 04.oracle对象&&05.oracle编程(a) 05.oracle编程(b) 04.orac ...