public void init(ServletConfig config) throws ServletException { super.init(config); try { String appPath = config.getServletContext().getRealPath(""); // 是否为开发mode debug = "true".equals(config.getInitParameter("development")); // 读取log4j.xml配置文件 DOMConfigurator.configure(appPath + "/WEB-INF/log4j.xml"); logger.info("Starting JForum. Debug mode is " + debug); // ConfigLoader.startSystemglobals(appPath); // 启动缓存引擎 ConfigLoader.startCacheEngine(); // Configure the template engine Configuration templateCfg = new Configuration(); templateCfg.setTemplateUpdateDelay(2); templateCfg.setSetting("number_format", "#"); templateCfg.setSharedVariable("startupTime", new Long(new Date().getTime())); // Create the default template loader String defaultPath = SystemGlobals.getApplicationPath() + "/templates"; FileTemplateLoader defaultLoader = new FileTemplateLoader(new File(defaultPath)); String extraTemplatePath = SystemGlobals.getValue(ConfigKeys.FREEMARKER_EXTRA_TEMPLATE_PATH); if (StringUtils.isNotBlank(extraTemplatePath)) { // An extra template path is configured, we need a MultiTemplateLoader FileTemplateLoader extraLoader = new FileTemplateLoader(new File(extraTemplatePath)); TemplateLoader[] loaders = new TemplateLoader[] {extraLoader, defaultLoader}; MultiTemplateLoader multiLoader = new MultiTemplateLoader(loaders); templateCfg.setTemplateLoader(multiLoader); } else { // An extra template path is not configured, we only need the default loader templateCfg.setTemplateLoader(defaultLoader); } // 载入模块 ModulesRepository.init(SystemGlobals.getValue(ConfigKeys.CONFIG_DIR)); this.loadConfigStuff(); if (!this.debug) { templateCfg.setTemplateUpdateDelay(3600); } JForumExecutionContext.setTemplateConfig(templateCfg); } catch (Exception e) { throw new ForumStartupException("Error while starting JForum", e); } }
FremarkParseUtils() { if (config == null) { config = new Configuration(); config.setTemplateLoader(new StringTemplateLoader()); try { config.setSetting("datetime_format", "yyyy-MM-dd HH:mm:ss"); config.setLocale(Locale.CHINA); } catch (Exception ex) { ex.printStackTrace(); } } }
/** 初始化freemarker */ private void initFreeMarker(ServletContextEvent sce) throws Exception { // freemarker上下文配置 Configuration freeMarkerConfig = new Configuration(Configuration.VERSION_2_3_23); freeMarkerConfig.setTemplateLoader(getTemplateLoader(sce)); freeMarkerConfig.setObjectWrapper( new DefaultObjectWrapperBuilder(Configuration.VERSION_2_3_23).build()); // freemaker setting freeMarkerConfig.setSetting("locale", "zh_CN"); freeMarkerConfig.setSetting("template_update_delay", "1"); freeMarkerConfig.setSetting("datetime_format", "yyyy-MM-dd HH:mm:ss"); freeMarkerConfig.setSetting("date_format", "HH:mm:ss"); freeMarkerConfig.setSetting("time_format", "yyyy-MM-dd"); freeMarkerConfig.setSetting("number_format", "#.##"); freeMarkerConfig.setSetting("default_encoding", "UTF-8"); freeMarkerConfig.setSetting("tag_syntax", "auto_detect"); freeMarkerConfig.setSetting("url_escaping_charset", "UTF-8"); sce.getServletContext().setAttribute(Constants.globalFreeMarkerConfig, freeMarkerConfig); }