zoukankan      html  css  js  c++  java
  • 继承ServletContextListener可以完成的事情

    1.定时任务:

      定时任务是从某个固定的时间开始执行特定的程序,继承这个方法,可以实现刚启动项目的时候执行某特定的程序,完成给客户部署的时即可以看到某个页面的效果。

    2.初始化系统常量等:

      这样来完成系统需要的一些数据,批量完成页面需要的某些变量,

      例如:Copyright © ${copyright_year}. ${applicationScope.cropyright_name}</div>

      而且这些元素是可以批量初始化到applicationScope中去的。所以有些项目根据jsp页面中的applicationScope.cropyright_name,全文查找是找不到页面中的元素在哪里存了。

      例如:

    public void contextInitialized(ServletContextEvent servletContextEvent) {

     ServletContext servletContext = servletContextEvent.getServletContext();
    ApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
    SysInformationMapper sysInformationMapper = applicationContext.getBean(SysInformationMapper.class);
    SysInformationExample sysInformationExample = new SysInformationExample();
    sysInformationExample.createCriteria().andEnabledEqualTo("1");
    List<SysInformation> sysInformationList = sysInformationMapper.selectByExample(sysInformationExample);

    if(sysInformationList!=null && sysInformationList.size()>0){
    for(SysInformation sysInformation:sysInformationList){
    servletContext.setAttribute(sysInformation.getInfoSign(),sysInformation.getInfoValue());
    System.out.println(sysInformation.getInfoSign()+" >>> "+sysInformation.getInfoValue());
    }
    }
    }

    3.需要在web.xml中配置对应的listener

    <listener>
    <listener-class>com.huntech.web.listener.SystemConfigListener</listener-class>
    </listener>
  • 相关阅读:
    == 与 equals 之区别
    值传递和引用传递
    try-catch-finally容易犯的错误
    设计模式之备忘录模式
    设计模式之观察者模式 Observer
    设计模式之状态模式 State
    设计模式之模板方法模式 templateMethod
    设计模式之策略模式 Strategy
    Java过滤器与SpringMVC拦截器之间的关系与区别
    SpringMVC中使用Interceptor拦截器
  • 原文地址:https://www.cnblogs.com/wlhebut/p/7889600.html
Copyright © 2011-2022 走看看