jsonConfig用法

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:<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;
    }
}


 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值