Spring在service层获取session和request

2020-12-13 13:42

阅读:580

Spring在service层获取session和request

时间:2019-09-17 12:44:27      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:Servle   frame   文件   bsp   attr   request   监听器   contex   class   

获取request有两种方法:

  • 1.在web.xml文件中添加监听器,然后通过注解注入request:

    
      class>org.springframework.web.context.request.RequestContextListenerclass>
    
    然后在service层注入直接注入对象
    @Autowired  
    private HttpSession session;  
    
    @Autowired  
    private HttpServletRequest request; 

     

  • 2.直接在service层普通类中获取

    HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();

Spring在service层获取session和request

标签:Servle   frame   文件   bsp   attr   request   监听器   contex   class   

原文地址:https://www.cnblogs.com/caozx/p/11506848.html


评论


亲,登录后才可以留言!