ssm 框架初始化 字典

Posted TT_DUL

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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

以上是关于ssm 框架初始化 字典的主要内容,如果未能解决你的问题,请参考以下文章

ssm初始化环境搭建

使用SSM框架开发企业级应用~Chapter01-初始MyBatis

SSM框架整合---完成对图书数据表的增删改查

优雅的ssm三大框架整合(全注解配置篇)转载

JavaWeb SSM SpringBoot商家订单管理系统《精品毕设》源码+论文)用户管理部门管理菜单管理角色管理字典管理操作日志生成管理等功能

SSM