public class ResInitListener implements ServletContextListener{
public void contextInitialized(ServletContextEvent event) {
//查找数据库中的全资源信息
WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(event.getServletContext());
ResEbi resEbi = (ResEbi) ctx.getBean("resEbi");
List<ResModel> resList = resEbi.getAll();
//将数据放入指定范围,备用
//数据存放以字符串形式存放
StringBuilder sb = new StringBuilder();
for(ResModel rm : resList){
sb.append(rm.getUrl());
sb.append(";");
}
event.getServletContext().setAttribute("allRes", sb.toString());
}
public void contextDestroyed(ServletContextEvent arg0) {
}
}
在web.xml中添加配置,需要添加在spring监听器的下面
<listener>
<listener-class>cn.itcast.invoice.util.listener.ResInitListener</listener-class>
</listener>