zoukankan      html  css  js  c++  java
  • jeecg中的一个上下文工具类获取request,session

    通过调用其中的方法可以获取到request和session,调用方式如下:

    HttpServletRequest request = ContextHolderUtils.getRequest();
    HttpSession session = ContextHolderUtils.getSession();
    ContextHolderUtils,这个类的源码如下:
    package org.jeecgframework.core.util;
    
    import java.util.HashMap;
    import java.util.Map;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpSession;
    import org.springframework.web.context.request.RequestContextHolder;
    import org.springframework.web.context.request.ServletRequestAttributes;
    
    /**
    * @ClassName: ContextHolderUtils 
    * @Description: TODO(上下文工具类) */
    public class ContextHolderUtils {
        /**
         * SpringMvc下获取request
         * 
         * @return
         */
        public static HttpServletRequest getRequest() {
            HttpServletRequest request = null;
            if (RequestContextHolder.getRequestAttributes()!=null) {
                request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            }
            return request;
    
        }
    
        /**
         * SpringMvc下获取session
         * 
         * @return
         */
        public static HttpSession getSession() {
            HttpServletRequest request = getRequest();
            String tempSessionId = request.getParameter("sessionId");
            HttpSession session = request.getSession();
            String sessionId = session.getId();
            if(StringUtil.isNotEmpty(tempSessionId) && !tempSessionId.equals(sessionId)){
                sessionId = tempSessionId;
                if(sessionMap.containsKey(sessionId)){
                    session = sessionMap.get(sessionId);
                }
            }
            if(!sessionMap.containsKey(sessionId)){
                sessionMap.put(sessionId, session);
            }
            return session;
        }
    
        
        private static final Map<String, HttpSession> sessionMap = new HashMap<String, HttpSession>();
        
        public static HttpSession getSession(String sessionId){
            HttpSession session = sessionMap.get(sessionId);
            return session == null ? getSession() : session;
        }
        
        public static void removeSession(String sessionId){
            if(sessionMap.containsKey(sessionId)){
                sessionMap.remove(sessionId);
            }
        }
    
    }
  • 相关阅读:
    echo "http://172.17.26.115:8380/?key=%c8%fd%d0%c7%ca%d6%bb%fa%b1%f9%cf%e4" | mail -s "noresult_monitr error" maolingzhi@jd.com
    python实现的文本编辑器
    PyQt写的浏览单web页面的browser
    中非发展基金
    团队介绍
    微众—国内最大的微信公众服务平台
    微软创投加速器简介
    知方可补不足~SQL为大数据引入分区表
    实时监控Cat之旅~介绍与自定义类型在哪里
    EF架构~Cannot attach the file as database
  • 原文地址:https://www.cnblogs.com/shuilangyizu/p/8621621.html
Copyright © 2011-2022 走看看