如何在任何地方调取Session对象

  • Post author:
  • Post category:其他


ssm中的用法

写一个普通类

package com.aaa.entity;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

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

/**
 * @program: 145ssm
 * @description:
 * @author: 
 * @create: 2021-12-10 09:26
 **/
public class WebUtil {

    public static HttpSession getSession(){
        HttpSession session = getRequest().getSession();
        return session;
    }

    public static HttpServletRequest getRequest(){
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        return request;
    }
}

其他地方直接调用就行

WebUtil.getSession().setAttribute("user",login);



版权声明:本文为m0_58984702原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。