jsonConfig用法
来源:互联网 发布:杨凯黄金分割线源码 编辑:程序博客网 时间:2024/06/07 09:33
1.先编写jsonConfig的初始化代码
private JsonConfigjsonConfig;
public action构造方法() {
jsonConfig = new JsonConfig();
jsonConfig.registerJsonValueProcessor(Date.class,newJsonValueProcessor() {
private SimpleDateFormatsdf = new SimpleDateFormat("yyyy-MM-ddHH:mm:ss");
@Override
public Object processObjectValue(String key,Object value, JsonConfig jsonConfig) {
returnthis.process(value);
}
@Override
public Object processArrayValue(Object value,JsonConfig arg1) {
returnthis.process(value);
}
// 处理Date类型返回的Json数值
private Object process(Object value) {
if (value ==null) {
return"";
} elseif (value instanceof Date)
returnsdf.format((Date)value);
else {
return value.toString();
}
}
});
// 不该传给前台的字段
jsonConfig.setJsonPropertyFilter(new PropertyFilter() {
public boolean apply(Objectsource, String name, Object value) {
if (sourceinstanceof RaffleLog) {
if ("contact".equals(name)) {
returntrue;
}
}
returnfalse;
}
});
}
/**
* @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的用法
- Json lib JsonConfig用法
- JsonConfig
- JsonConfig
- JsonConfig
- jsonConfig配置
- jsonconfig详解
- jsonConfig使用
- jsonConfig详细使用
- JsonConfig处理日期
- JSONLib-JsonConfig探讨
- json-lib jsonConfig操作
- Json-lib JsonConfig
- JSON 中JsonConfig的使用
- JSON 中JsonConfig的使用
- JSON 中JsonConfig的使用
- JsonConfig的常见的方法
- 【Android 应用开发】Android开发 使用 adb logcat 显示 Android 日志
- Java加密技术(一)——BASE64与单向加密算法MD5&SHA&MAC
- zookeeper应用场景之配置文件同步
- Java 泛型学习(一)简单使用
- 个人电脑做网站服务器
- jsonConfig用法
- python的gzip库使用方法
- 红黑树-JAVA Programming
- adb uninstall
- Huffuman树
- JAVA读取WORD,EXCEL,POWERPOINT,PDF文件的方法
- Hadoop关于处理大量小文件的问题和解决方法
- 历史上最知名的15位计算机科学家
- 常用排序、查找算法java实现