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);  

  } 

猜你喜欢

转载自zhitangrui2010.iteye.com/blog/2240038
今日推荐