jsonConfig用法

前端之家收集整理的这篇文章主要介绍了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,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,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;
    }
}
原文链接:https://www.f2er.com/json/290214.html

猜你在找的Json相关文章