Ajax常用工具

来源:互联网 发布:sql数据库语法 编辑:程序博客网 时间:2024/06/15 03:42



import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;


import javax.servlet.http.HttpServletResponse;


import net.sf.json.JSONObject;


public class AjaxUtil 
{
     
public static String AJAX_RETURN_OBJECT="ajaxReturnObject";//dont change this



public static String AJAX_RETURN_VIEW="ajaxReturnView";//dont change this

    public static String bean2Json(Object bean){
        JSONObject jsonobj=JSONObject.fromObject(bean);
        String jsonStr=jsonobj.toString();
        return jsonStr;
    }
    
    public static void printf(AjaxResponseDTO bean, HttpServletResponse response) {
response.setContentType("text/Xml;charset=UTF-8");
response.setHeader("Pragma", "no-cache");
response.addHeader("Cache-Control", "must-revalidate");
response.addHeader("Cache-Control", "no-cache");
response.addHeader("Cache-Control", "no-store");
response.setDateHeader("Expires", 0);
PrintWriter out = null;


try {
out = response.getWriter();
out.print(AjaxUtil.bean2Json(bean));
} catch (IOException e) {
} finally {
if (null != out)
out.close();
}


}
     
   public static String listToJson(List<?> list) {     
        StringBuilder json = new StringBuilder();
        json.append("[");     
       if (list != null && list.size() > 0) {     
           for (Object obj : list) {     
                json.append(bean2Json(obj));     
                json.append(",");     
            }     
            json.setCharAt(json.length() - 1, ']');     
        } else {     
            json.append("]");     
        }     
       return json.toString();     
    }  


}





0 0