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. ORACLE增删改查以及case when的基本用法

    1.创建table create table test01( id int not null primary key, name ) not null, gender ) not null, age ...

  2. 【转帖】使用容器化和 Docker 实现 DevOps 的基础知识

    使用容器化和 Docker 实现 DevOps 的基础知识 https://www.kubernetes.org.cn/6730.html 2020-02-24 15:20 灵雀云 分类:容器 阅读( ...

  3. 20个GitHub最热门的Java开源项目:文档、框架、工具

    专注于Java领域优质技术,欢迎关注 文章来源:JavaGuide 以下涉及到的数据统计,数据来源:https://github.com/trending/java?since=monthly[1] ...

  4. Mybatis之二级缓存(八)

    1. 介绍 Mybatis缓存分为一级缓存和二级缓存,在本节中我们介绍下二级缓存的使用及其特性 MyBatis的一级缓存是在一个Session域内有效的,当Session关闭后,缓存内容也随之销毁.但 ...

  5. tensorflow常用函数库

    归一化函数: def norm_boxes(boxes, shape): """Converts boxes from pixel coordinates to norm ...

  6. 深入浅出 java.String

    深入浅出 java.String Java 处理字符串常用的一些方法 Java定义一个字符串 直接定字符串 直接定义字符串表示直接使用""来表示字符串中的内容 String str ...

  7. Tensorflow函数——tf.set_random_seed(seed)

    设置图级随机seed. 依赖于随机seed的操作实际上从两个seed中获取:图级和操作级seed. 这将设置图级别的seed. 其与操作级seed的相互作用如下: 1.如果没有设置图形级别和操作see ...

  8. vue每次运行起来端口不一致问题

    原因:portfinder新发布的版本异常 解决方案:npm install portfinder@1.0.21

  9. 吴裕雄--天生自然 JAVA开发学习:switch case 语句

    public class Test { public static void main(String args[]){ //char grade = args[0].charAt(0); char g ...

  10. PAT Basic 1034 有理数四则运算(20) [数学问题-分数的四则运算]

    题目 本题要求编写程序,计算2个有理数的和.差.积.商. 输⼊格式: 输⼊在⼀⾏中按照"a1/b1 a2/b2"的格式给出两个分数形式的有理数,其中分⼦和分⺟全是整型范围内的整数, ...