1.在action里准备页面显示的数据,但因不需每次都重新准备,所以我们在listener里将数据放在application中 2.新建一个listener(initListener),继承servletconfiglistener
public class initServletContextListener implements ServletContextListener {
public void contextDestroyed(ServletContextEvent arg0) {
}
public void contextInitialized(ServletContextEvent arg0) {
ServletContext application=arg0.getServletContext();
ApplicationContext ac=WebApplicationContextUtils.getWebApplicationContext(application);
PrivilegeService privilegeService=(PrivilegeService) ac.getBean(" privilegeServiceImpl");
List<Privilege> topPrivilegeList=privilegeService.findTopList();
application.setAttribute("topPrivilegeList", topPrivilegeList);
System.out.println("-------权限的顶级对象已经准备好了--------");
}
}
3.在web.xml中配置listener
<listener>
<listener-class>cn.itcast.oa.listener.initServletContextListener</listener-class>
</listener>
4.在页面使用application即可