Struts2工具类[实现获取Request/Response/Session与绕过jsp/freemaker直接输出文本的简化函数]
来源:互联网 发布:淘宝发货流程查看 编辑:程序博客网 时间:2024/06/05 18:27
- package org.eline.web.struts2;
-
- import java.io.IOException;
- import java.util.Map;
-
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import javax.servlet.http.HttpSession;
-
- import net.sf.json.JSONObject;
-
- import org.apache.commons.lang.StringUtils;
- import org.apache.struts2.ServletActionContext;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
-
-
-
-
-
- public class Struts2Utils
- {
-
-
- private static final String ENCODING_PREFIX = "encoding:";
- private static final String NOCACHE_PREFIX = "no-cache:";
- private static final String ENCODING_DEFAULT = "UTF-8";
- private static final boolean NOCACHE_DEFAULT = true;
-
- private static Logger logger = LoggerFactory.getLogger(Struts2Utils.class);
-
- private Struts2Utils()
- {
- }
-
-
-
-
-
-
- public static HttpSession getSession()
- {
- return ServletActionContext.getRequest().getSession();
- }
-
-
-
-
- public static HttpServletRequest getRequest()
- {
- return ServletActionContext.getRequest();
- }
-
-
-
-
- public static HttpServletResponse getResponse()
- {
- return ServletActionContext.getResponse();
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public static void render(final String contentType, final String content,
- final String... headers)
- {
- try
- {
-
- String encoding = ENCODING_DEFAULT;
- boolean noCache = NOCACHE_DEFAULT;
- for (String header : headers)
- {
- String headerName = StringUtils.substringBefore(header, ":");
- String headerValue = StringUtils.substringAfter(header, ":");
-
- if (StringUtils.equalsIgnoreCase(headerName, ENCODING_PREFIX))
- {
- encoding = headerValue;
- }
- else if (StringUtils.equalsIgnoreCase(headerName,
- NOCACHE_PREFIX))
- {
- noCache = Boolean.parseBoolean(headerValue);
- }
- else
- throw new IllegalArgumentException(headerName
- + "不是一个合法的header类型");
- }
-
- HttpServletResponse response = ServletActionContext.getResponse();
-
-
- String fullContentType = contentType + ";charset=" + encoding;
- response.setContentType(fullContentType);
- if (noCache)
- {
- response.setHeader("Pragma", "No-cache");
- response.setHeader("Cache-Control", "no-cache");
- response.setDateHeader("Expires", 0);
- }
-
- response.getWriter().write(content);
-
- }
- catch (IOException e)
- {
- logger.error(e.getMessage(), e);
- }
- }
-
-
-
-
-
-
- public static void renderText(final String text, final String... headers)
- {
- render("text/plain", text, headers);
- }
-
-
-
-
-
-
- public static void renderHtml(final String html, final String... headers)
- {
- render("text/html", html, headers);
- }
-
-
-
-
-
-
- public static void renderXml(final String xml, final String... headers)
- {
- render("text/xml", xml, headers);
- }
-
-
-
-
-
-
-
-
- public static void renderJson(final String string, final String... headers)
- {
- render("application/json", string, headers);
- }
-
-
-
-
-
-
-
-
- @SuppressWarnings("unchecked")
- public static void renderJson(final Map map, final String... headers)
- {
- String jsonString = new JSONObject().toString();
- renderJson(jsonString, headers);
- }
-
-
-
-
-
-
-
-
- public static void renderJson(final Object object, final String... headers)
- {
- String jsonString = new JSONObject().toString();
- renderJson(jsonString, headers);
- }
- }