//继承 ServletContextListener
public class InitDicListener implements ServletContextListener {
public void contextInitialized(ServletContextEvent sce) {
//spring 上下文
WebApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(sce.getServletContext());
/*
加载数据字典
*/
DictService initDictBean = (DictService) appContext.getBean("initDictBean");
List<EnumValue> initDict = initDictBean.initDict();
Iterator<EnumValue> iterator = initDict.iterator();
HashMap<Integer,HashMap<Integer,String>> dict = new HashMap<Integer, HashMap<Integer, String>>();
HashMap<Integer,String> hashMap = new HashMap<Integer,String>();
HashMap<Integer, ArrayList<EnumValue>> dictList = new HashMap<Integer, ArrayList<EnumValue>>();
ArrayList<EnumValue> dic = new ArrayList<EnumValue>();
if(iterator.hasNext()){
EnumValue next = iterator.next();
int type=next.getType();
//加map类型 字典
hashMap.put(next.getValue(), next.getName());
//加list类型 字典
dic.add(next);
while(iterator.hasNext()){
next = iterator.next();
Integer type2 = next.getType();
if(type2!=type){
//加map类型 字典
dict.put(type, hashMap);
hashMap = new HashMap<Integer,String>();
hashMap.put(next.getValue(), next.getName());
//加list类型 字典
dictList.put(type, dic);
dic = new ArrayList<EnumValue>();
dic.add(next);
type=type2;
}else{
//加map类型 字典
hashMap.put(next.getValue(), next.getName());
//加list类型 字典
dic.add(next);
}
}
dict.put(type, hashMap);
dictList.put(type, dic);
}
// dict.put(12, waybillFinishState);
ServletContext context = sce.getServletContext();
context.setAttribute("dict", dict);
context.setAttribute("dictList", dictList);
context.setAttribute("dictAll", initDict);
}
public void contextDestroyed(ServletContextEvent sce) {
ServletContext context = sce.getServletContext();
context.removeAttribute("dict");
}
}
ApplicationContext 中配置
<listener>
<listener-class>org.springframework.web.util.WebAppRootListener</listener- class>
</listener>
要放在WebAppRootListener下面
<listener>
<listener-class>com.vshop.sys.control.InitDicListener</listener-class>
</listener>
control 层中直接可以用了
HashMap