ssm 框架初始化 字典

//继承 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

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值