创建好的BaseAction

创建好的 BaseAction , 在其中实现常用的方法,开发就比较轻松.

 

我们项目中实现基本的 request , response 获得,  Json 数据转换 。

 

/**
 * Action基类
 *
 */
public class BaseAction extends ActionSupport implements ServletRequestAware, ServletResponseAware {


    /**
     * 返回json字符串
     */
    protected void renderJSON(Map map) {
        JSONObject jsonObjectFromMap = JSONObject.fromObject(map);
        renderText(jsonObjectFromMap.toString());

    }


    /**
     * 直接输出字符�
     */
    protected String renderText(String text) {
        return render(text, "text/plain;charset=UTF-8");
    }

    /**
     * 直接输出HTML.
     */
    protected String renderHtml(String html) {
        return render(html, "text/html;charset=UTF-8");
    }

    /**
     * 直接输出XML.
     */
    protected String renderXML(String xml) {
        return render(xml, "text/xml;charset=UTF-8");
    }
	/**
	 * Description: 直接输出Text
	 * @param result 输出的内容  removeCache 是否清楚cache
	 * @return 
	 */
	protected void writeResult(String result, boolean removeCache) {
		PrintWriter writer = null;
		try {
			if(removeCache) {
				response.setHeader("Pragma", "No-cache");// 清除缓存
				response.setHeader("Cache-Control", "no-cache");
				response.setDateHeader("Expires", 0);
			}
			response.setContentType("text/html; charset=utf-8");
			writer = response.getWriter();
			writer.print(result);
			writer.flush();
		} catch (IOException e) {
		} finally {
			if (writer != null) {
				writer.close();
			}
		}
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	@Override
	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}

}
	
 

 

 

 

你可能感兴趣的:(html,json,xml,cache)