博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
jsonConfig使用方法
阅读量:6939 次
发布时间:2019-06-27

本文共 5118 字,大约阅读时间需要 17 分钟。

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:
* @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
> returnType = newReturnType
>(); 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; }}

 

 

转载地址:http://bhinl.baihongyu.com/

你可能感兴趣的文章
Mysqlhotcopy热备本工具介绍
查看>>
ubuntu1204 source list
查看>>
Stylish 样式
查看>>
各种大型网站技术架构
查看>>
MySQL + MHA + keepalive + VIP 高可用实验
查看>>
exchange2010 移动邮箱
查看>>
使用syslog-ng搭建日志服务器
查看>>
跨域 Cookie 实现单点登录
查看>>
Facade模式[fəˈsɑːd]
查看>>
PVS架构之VHD虚拟磁盘 二
查看>>
ECS centos6.5安装redis3.0.7单机版
查看>>
我的友情链接
查看>>
Linux下安装jdk报Permission denied以及chmod详解
查看>>
Python搭建环境
查看>>
Solr环境搭建及IK分词的集成及solrJ的调用(二)
查看>>
No code, no money; No defect, no job.
查看>>
NO3
查看>>
LVS--DR模型
查看>>
微信扫码支付
查看>>
CentOS7.0 下sphinx搭建
查看>>