直接获取HttpServletRequest和HttpSession对象
程序员文章站
2024-01-14 21:25:22
...
为了省事省心不多传一个参数,就直接获取操作就好了!
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
public class Test{
//以下两步可以直接合成一步
//HttpSession session = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getSession();
//1、直接获取HttpServletRequest
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
//2、获取session
HttpSession session = request.getSession();
}
推荐阅读