Struts2工具类[实现获取Request/Response/Session与绕过jsp/freemaker直接输出文本的简化函数]

来源:互联网 发布:淘宝发货流程查看 编辑:程序博客网 时间:2024/06/05 18:27
[java] view plaincopy
  1. package org.eline.web.struts2;  
  2.   
  3. import java.io.IOException;  
  4. import java.util.Map;  
  5.   
  6. import javax.servlet.http.HttpServletRequest;  
  7. import javax.servlet.http.HttpServletResponse;  
  8. import javax.servlet.http.HttpSession;  
  9.   
  10. import net.sf.json.JSONObject;  
  11.   
  12. import org.apache.commons.lang.StringUtils;  
  13. import org.apache.struts2.ServletActionContext;  
  14. import org.slf4j.Logger;  
  15. import org.slf4j.LoggerFactory;  
  16.   
  17. /** 
  18.  * Struts2 Utils类. 
  19.  * 实现获取Request/Response/Session与绕过jsp/freemaker直接输出文本的简化函数. 
  20.  */  
  21. public class Struts2Utils  
  22. {  
  23.   
  24.     // header 常量定义  
  25.     private static final String ENCODING_PREFIX = "encoding:";  
  26.     private static final String NOCACHE_PREFIX = "no-cache:";  
  27.     private static final String ENCODING_DEFAULT = "UTF-8";  
  28.     private static final boolean NOCACHE_DEFAULT = true;  
  29.   
  30.     private static Logger logger = LoggerFactory.getLogger(Struts2Utils.class);  
  31.   
  32.     private Struts2Utils()  
  33.     {  
  34.     }  
  35.   
  36.     // 取得Request/Response/Session的简化函数 //  
  37.   
  38.     /** 
  39.      * 取得HttpSession的简化方法. 
  40.      */  
  41.     public static HttpSession getSession()  
  42.     {  
  43.         return ServletActionContext.getRequest().getSession();  
  44.     }  
  45.   
  46.     /** 
  47.      * 取得HttpRequest的简化方法. 
  48.      */  
  49.     public static HttpServletRequest getRequest()  
  50.     {  
  51.         return ServletActionContext.getRequest();  
  52.     }  
  53.   
  54.     /** 
  55.      * 取得HttpResponse的简化方法. 
  56.      */  
  57.     public static HttpServletResponse getResponse()  
  58.     {  
  59.         return ServletActionContext.getResponse();  
  60.     }  
  61.   
  62.     // 绕过jsp/freemaker直接输出文本的函数 //  
  63.   
  64.     /** 
  65.      * 直接输出内容的简便函数. 
  66.      *  
  67.      * eg. render("text/plain", "hello", "encoding:GBK"); render("text/plain", 
  68.      * "hello", "no-cache:false"); render("text/plain", "hello", "encoding:GBK", 
  69.      * "no-cache:false"); 
  70.      *  
  71.      * @param headers 
  72.      *            可变的header数组,目前接受的值为"encoding:"或"no-cache:",见示例代码. 
  73.      *            不设置时默认值分别为UTF-8和true. 
  74.      */  
  75.     public static void render(final String contentType, final String content,  
  76.             final String... headers)  
  77.     {  
  78.         try  
  79.         {  
  80.             // 分析headers参数  
  81.             String encoding = ENCODING_DEFAULT;  
  82.             boolean noCache = NOCACHE_DEFAULT;  
  83.             for (String header : headers)  
  84.             {  
  85.                 String headerName = StringUtils.substringBefore(header, ":");  
  86.                 String headerValue = StringUtils.substringAfter(header, ":");  
  87.   
  88.                 if (StringUtils.equalsIgnoreCase(headerName, ENCODING_PREFIX))  
  89.                 {  
  90.                     encoding = headerValue;  
  91.                 }  
  92.                 else if (StringUtils.equalsIgnoreCase(headerName,  
  93.                         NOCACHE_PREFIX))  
  94.                 {  
  95.                     noCache = Boolean.parseBoolean(headerValue);  
  96.                 }  
  97.                 else  
  98.                     throw new IllegalArgumentException(headerName  
  99.                             + "不是一个合法的header类型");  
  100.             }  
  101.   
  102.             HttpServletResponse response = ServletActionContext.getResponse();  
  103.   
  104.             // 设置headers参数  
  105.             String fullContentType = contentType + ";charset=" + encoding;  
  106.             response.setContentType(fullContentType);  
  107.             if (noCache)  
  108.             {  
  109.                 response.setHeader("Pragma""No-cache");  
  110.                 response.setHeader("Cache-Control""no-cache");  
  111.                 response.setDateHeader("Expires"0);  
  112.             }  
  113.   
  114.             response.getWriter().write(content);  
  115.   
  116.         }  
  117.         catch (IOException e)  
  118.         {  
  119.             logger.error(e.getMessage(), e);  
  120.         }  
  121.     }  
  122.   
  123.     /** 
  124.      * 直接输出文本. 
  125.      *  
  126.      * @see #render(String, String, String...) 
  127.      */  
  128.     public static void renderText(final String text, final String... headers)  
  129.     {  
  130.         render("text/plain", text, headers);  
  131.     }  
  132.   
  133.     /** 
  134.      * 直接输出HTML. 
  135.      *  
  136.      * @see #render(String, String, String...) 
  137.      */  
  138.     public static void renderHtml(final String html, final String... headers)  
  139.     {  
  140.         render("text/html", html, headers);  
  141.     }  
  142.   
  143.     /** 
  144.      * 直接输出XML. 
  145.      *  
  146.      * @see #render(String, String, String...) 
  147.      */  
  148.     public static void renderXml(final String xml, final String... headers)  
  149.     {  
  150.         render("text/xml", xml, headers);  
  151.     }  
  152.   
  153.     /** 
  154.      * 直接输出JSON. 
  155.      *  
  156.      * @param string 
  157.      *            json字符串. 
  158.      * @see #render(String, String, String...) 
  159.      */  
  160.     public static void renderJson(final String string, final String... headers)  
  161.     {  
  162.         render("application/json", string, headers);  
  163.     }  
  164.   
  165.     /** 
  166.      * 直接输出JSON.???/ 
  167.      *  
  168.      * @param map 
  169.      *            Map对象,将被转化为json字符串. 
  170.      * @see #render(String, String, String...) 
  171.      */  
  172.     @SuppressWarnings("unchecked")  
  173.     public static void renderJson(final Map map, final String... headers)  
  174.     {  
  175.         String jsonString = new JSONObject().toString();  
  176.         renderJson(jsonString, headers);  
  177.     }  
  178.   
  179.     /** 
  180.      * 直接输出JSON.??? 
  181.      *  
  182.      * @param object 
  183.      *            Java对象,将被转化为json字符串. 
  184.      * @see #render(String, String, String...) 
  185.      */  
  186.     public static void renderJson(final Object object, final String... headers)  
  187.     {  
  188.         String jsonString = new JSONObject().toString();  
  189.         renderJson(jsonString, headers);  
  190.     }