加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

Struts2工具类

发布时间:2020-12-14 23:18:59 所属栏目:Java 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 简化获取response,request及输出操作 import java.io.IOException;import java.util.Map;import javax.servlet.http.HttpServletRequest;import java

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

简化获取response,request及输出操作
import java.io.IOException;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Struts2 Utils类.
 * 
 * 实现获取Request/Response/Session与绕过jsp/freemaker直接输出文本的简化函数.
 * 
 */
public class Struts2Utils {

        //header 常量定义
        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() {
        }

        // 取得Request/Response/Session的简化函数 //

        /**
         * 取得HttpSession的简化方法.
         */
        public static HttpSession getSession() {
                return ServletActionContext.getRequest().getSession();
        }

        /**
         * 取得HttpRequest的简化方法.
         */
        public static HttpServletRequest getRequest() {
                return ServletActionContext.getRequest();
        }

        /**
         * 取得HttpResponse的简化方法.
         */
        public static HttpServletResponse getResponse() {
                return ServletActionContext.getResponse();
        }

        /**
         * 取得Request Parameter的简化方法.
         */
        public static String getParameter(String name) {
                return getRequest().getParameter(name);
        }

        // 绕过jsp/freemaker直接输出文本的函数 //

        /**
         * 直接输出内容的简便函数.
          
         * eg.
         * render("text/plain","hello","encoding:GBK");
         * render("text/plain","no-cache:false");
         * render("text/plain","encoding:GBK","no-cache:false");
         * 
         * @param headers 可变的header数组,目前接受的值为"encoding:"或"no-cache:",默认值分别为UTF-8和true.                 
         */
        public static void render(final String contentType,final String content,final String... headers) {
                try {
                        //分析headers参数
                        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();

                        //设置headers参数
                        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);
        }

        /**
         * 直接输出HTML.
         */
        public static void renderHtml(final String html,final String... headers) {
                render("text/html",html,headers);
        }

        /**
         * 直接输出XML.
         */
        public static void renderXml(final String xml,final String... headers) {
                render("text/xml",xml,headers);
        }

        /**
         * 直接输出JSON.
         * 
         * @param string json字符串.
         */
        public static void renderJson(final String string,final String... headers) {
                render("application/json",string,headers);
        }

        /**
         * 直接输出JSON.
         * 
         * @param map Map对象,将被转化为json字符串.
         */
        @SuppressWarnings("unchecked")
        public static void renderJson(final Map map,final String... headers) {
                String jsonString = new JSONObject(map).toString();
                renderJson(jsonString,headers);
        }

        /**
         * 直接输出JSON.
         * 
         * @param object Java对象,将被转化为json字符串.
         */
        public static void renderJson(final Object object,final String... headers) {
                String jsonString = new JSONObject(object).toString();
                renderJson(jsonString,headers);
        }
}

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读