关于监听器的使用(服务器启动时加载数据)

来源:互联网 发布:腾讯数据分析待遇 编辑:程序博客网 时间:2024/05/22 11:01
1.在页面加载时,如果该页面存在下拉框 ,这个下拉框的值是从数据库读取的,这样会影响页面加载的速度,此时可以使用一监听器,让服务器启动的时候就查询(加载这些数据),需要配置的文件如下:
自定义的监听器:
public class InitDictListener implements ServletContextListener{

     @Override
     public void contextDestroyed(ServletContextEvent event) {
         
     }

     @Override
     public void contextInitialized(ServletContextEvent event) {
          ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(event.getServletContext());
          ProductRegisterExaService bean = (ProductRegisterExaService)appContext.getBean(ServicesNames.PRODUCT_REGISTER_EXA_SERVICE);
          //查询品种分类数据加载到缓存中
          List<CatHcCatalog> list = bean.queryCatHcCategoryList(null);//去查询相关的数据
          event.getServletContext().setAttribute("catalogList", list);//catalogList是在jsp页面中遍历取值
          //查询品种分类数据加载到缓存中
     }
    
}


对于web.xml:

<listener>
     <listener-class>
          com.comm.listener.InitDictListener
     </listener-class>
</listener>     
0 0
原创粉丝点击