`

struts2用cookie实现自动登录中用过滤器执行读取上下文方法

 
阅读更多

HttpSession session = request.getSession(); 

        IUserDao ud=(IUserDao)this.getObjectFromApplication(session.getServletContext(), "IUserDao");

执行后ud即可直接调用其中的方法

 

/** 

   * 通过WebApplicationContextUtils 得到Spring容器的实例。根据bean的名称返回bean的实例。 

   * @param servletContext  :ServletContext上下文。 

   * @param beanName  :要取得的Spring容器中Bean的名称。 

   * @return 返回Bean的实例。 

   */  

  private Object getObjectFromApplication(ServletContext servletContext,String beanName){  

      //通过WebApplicationContextUtils 得到Spring容器的实例。  

      ApplicationContext application=WebApplicationContextUtils.getWebApplicationContext(servletContext);  

      //返回Bean的实例。  

      return application.getBean(beanName);  

  } 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics