ITeye博客

action返回JSON数据结构的做法

iammonster · 2009-11-02 18:08 · Ajax · Markdown

action返回 JSON数据结构,做个笔记。

您是不是感觉有些没注意到的地方呢?

 

    /**
     * 创建AJAX应答数据(JSON数据格式)
     * 
     * @param response
     *            输出
     * @param obj
     *            要封装的对象
     * @return null
     * @throws IOException
     */
    private static ActionForward buildJSONDate(final HttpServletResponse response, final Object obj) throws IOException
    {
       //设置编码
        response.setContentType("application/json;charset=UTF-8");
        response.setCharacterEncoding("UTF-8");

        // 设置浏览器不要缓存
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);

        final PrintWriter out = response.getWriter();

        JsonUtil.toJson(out, obj); //把JSON数据结构写到输出流中

        out.flush();

        return null;
    }

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。