Java Code Examples for freemarker.template.Configuration#setAllSharedVariables()
The following examples show how to use
freemarker.template.Configuration#setAllSharedVariables() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FreemarkerConfigurationBuilder.java From ogham with Apache License 2.0 | 5 votes |
private void buildSharedVariables(Configuration configuration) { try { if (variablesHash != null) { configuration.setAllSharedVariables(variablesHash); } for (Entry<String, Object> entry : sharedVariables.entrySet()) { configuration.setSharedVariable(entry.getKey(), entry.getValue()); } } catch (TemplateModelException e) { throw new BuildException("Failed to configure FreeMarker shared variables", e); } }
Example 2
Source File: SiteContextResolvingFilter.java From engine with GNU General Public License v3.0 | 5 votes |
protected void renderError(HttpServletResponse response) { response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); try { Configuration configuration = freeMarkerConfigFactory.getObject().getConfiguration(); Template template = configuration.getTemplate(errorTemplate); SimpleHash model = new SimpleHash(configuration.getObjectWrapper()); configuration.setAllSharedVariables(model); template.process(model, response.getWriter()); } catch (Exception e) { logger.error("Error rendering template for site resolving error", e); } }
Example 3
Source File: FreeMarkerConfigurationFactory.java From spring-analysis-note with MIT License | 4 votes |
/** * Prepare the FreeMarker Configuration and return it. * @return the FreeMarker Configuration object * @throws IOException if the config file wasn't found * @throws TemplateException on FreeMarker initialization failure */ public Configuration createConfiguration() throws IOException, TemplateException { Configuration config = newConfiguration(); Properties props = new Properties(); // Load config file if specified. if (this.configLocation != null) { if (logger.isDebugEnabled()) { logger.debug("Loading FreeMarker configuration from " + this.configLocation); } PropertiesLoaderUtils.fillProperties(props, this.configLocation); } // Merge local properties if specified. if (this.freemarkerSettings != null) { props.putAll(this.freemarkerSettings); } // FreeMarker will only accept known keys in its setSettings and // setAllSharedVariables methods. if (!props.isEmpty()) { config.setSettings(props); } if (!CollectionUtils.isEmpty(this.freemarkerVariables)) { config.setAllSharedVariables(new SimpleHash(this.freemarkerVariables, config.getObjectWrapper())); } if (this.defaultEncoding != null) { config.setDefaultEncoding(this.defaultEncoding); } List<TemplateLoader> templateLoaders = new ArrayList<>(this.templateLoaders); // Register template loaders that are supposed to kick in early. if (this.preTemplateLoaders != null) { templateLoaders.addAll(this.preTemplateLoaders); } // Register default template loaders. if (this.templateLoaderPaths != null) { for (String path : this.templateLoaderPaths) { templateLoaders.add(getTemplateLoaderForPath(path)); } } postProcessTemplateLoaders(templateLoaders); // Register template loaders that are supposed to kick in late. if (this.postTemplateLoaders != null) { templateLoaders.addAll(this.postTemplateLoaders); } TemplateLoader loader = getAggregateTemplateLoader(templateLoaders); if (loader != null) { config.setTemplateLoader(loader); } postProcessConfiguration(config); return config; }
Example 4
Source File: FreeMarkerConfigurationFactory.java From java-technology-stack with MIT License | 4 votes |
/** * Prepare the FreeMarker Configuration and return it. * @return the FreeMarker Configuration object * @throws IOException if the config file wasn't found * @throws TemplateException on FreeMarker initialization failure */ public Configuration createConfiguration() throws IOException, TemplateException { Configuration config = newConfiguration(); Properties props = new Properties(); // Load config file if specified. if (this.configLocation != null) { if (logger.isDebugEnabled()) { logger.debug("Loading FreeMarker configuration from " + this.configLocation); } PropertiesLoaderUtils.fillProperties(props, this.configLocation); } // Merge local properties if specified. if (this.freemarkerSettings != null) { props.putAll(this.freemarkerSettings); } // FreeMarker will only accept known keys in its setSettings and // setAllSharedVariables methods. if (!props.isEmpty()) { config.setSettings(props); } if (!CollectionUtils.isEmpty(this.freemarkerVariables)) { config.setAllSharedVariables(new SimpleHash(this.freemarkerVariables, config.getObjectWrapper())); } if (this.defaultEncoding != null) { config.setDefaultEncoding(this.defaultEncoding); } List<TemplateLoader> templateLoaders = new ArrayList<>(this.templateLoaders); // Register template loaders that are supposed to kick in early. if (this.preTemplateLoaders != null) { templateLoaders.addAll(this.preTemplateLoaders); } // Register default template loaders. if (this.templateLoaderPaths != null) { for (String path : this.templateLoaderPaths) { templateLoaders.add(getTemplateLoaderForPath(path)); } } postProcessTemplateLoaders(templateLoaders); // Register template loaders that are supposed to kick in late. if (this.postTemplateLoaders != null) { templateLoaders.addAll(this.postTemplateLoaders); } TemplateLoader loader = getAggregateTemplateLoader(templateLoaders); if (loader != null) { config.setTemplateLoader(loader); } postProcessConfiguration(config); return config; }
Example 5
Source File: FreeMarkerConfigurationFactory.java From lams with GNU General Public License v2.0 | 4 votes |
/** * Prepare the FreeMarker Configuration and return it. * @return the FreeMarker Configuration object * @throws IOException if the config file wasn't found * @throws TemplateException on FreeMarker initialization failure */ public Configuration createConfiguration() throws IOException, TemplateException { Configuration config = newConfiguration(); Properties props = new Properties(); // Load config file if specified. if (this.configLocation != null) { if (logger.isInfoEnabled()) { logger.info("Loading FreeMarker configuration from " + this.configLocation); } PropertiesLoaderUtils.fillProperties(props, this.configLocation); } // Merge local properties if specified. if (this.freemarkerSettings != null) { props.putAll(this.freemarkerSettings); } // FreeMarker will only accept known keys in its setSettings and // setAllSharedVariables methods. if (!props.isEmpty()) { config.setSettings(props); } if (!CollectionUtils.isEmpty(this.freemarkerVariables)) { config.setAllSharedVariables(new SimpleHash(this.freemarkerVariables, config.getObjectWrapper())); } if (this.defaultEncoding != null) { config.setDefaultEncoding(this.defaultEncoding); } List<TemplateLoader> templateLoaders = new LinkedList<TemplateLoader>(this.templateLoaders); // Register template loaders that are supposed to kick in early. if (this.preTemplateLoaders != null) { templateLoaders.addAll(this.preTemplateLoaders); } // Register default template loaders. if (this.templateLoaderPaths != null) { for (String path : this.templateLoaderPaths) { templateLoaders.add(getTemplateLoaderForPath(path)); } } postProcessTemplateLoaders(templateLoaders); // Register template loaders that are supposed to kick in late. if (this.postTemplateLoaders != null) { templateLoaders.addAll(this.postTemplateLoaders); } TemplateLoader loader = getAggregateTemplateLoader(templateLoaders); if (loader != null) { config.setTemplateLoader(loader); } postProcessConfiguration(config); return config; }
Example 6
Source File: WordprocessingMLFreemarkerTemplate.java From docx4j-template with Apache License 2.0 | 4 votes |
protected Configuration getInternalEngine() throws IOException, TemplateException{ try { BeansWrapper beansWrapper = new BeansWrapper(Configuration.VERSION_2_3_23); this.templateModel = beansWrapper.getStaticModels().get(String.class.getName()); } catch (TemplateModelException e) { throw new IOException(e.getMessage(),e.getCause()); } // 创建 Configuration 实例 Configuration config = new Configuration(Configuration.VERSION_2_3_23); Properties props = ConfigUtils.filterWithPrefix("docx4j.freemarker.", "docx4j.freemarker.", Docx4jProperties.getProperties(), false); // FreeMarker will only accept known keys in its setSettings and // setAllSharedVariables methods. if (!props.isEmpty()) { config.setSettings(props); } if (this.freemarkerVariables != null && !this.freemarkerVariables.isEmpty()) { config.setAllSharedVariables(new SimpleHash(this.freemarkerVariables, config.getObjectWrapper())); } if (this.defaultEncoding != null) { config.setDefaultEncoding(this.defaultEncoding); } List<TemplateLoader> templateLoaders = new LinkedList<TemplateLoader>(this.templateLoaders); // Register template loaders that are supposed to kick in early. if (this.preTemplateLoaders != null) { templateLoaders.addAll(this.preTemplateLoaders); } postProcessTemplateLoaders(templateLoaders); // Register template loaders that are supposed to kick in late. if (this.postTemplateLoaders != null) { templateLoaders.addAll(this.postTemplateLoaders); } TemplateLoader loader = getAggregateTemplateLoader(templateLoaders); if (loader != null) { config.setTemplateLoader(loader); } //config.setClassLoaderForTemplateLoading(classLoader, basePackagePath); //config.setCustomAttribute(name, value); //config.setDirectoryForTemplateLoading(dir); //config.setServletContextForTemplateLoading(servletContext, path); //config.setSharedVariable(name, value); //config.setSharedVariable(name, tm); config.setSharedVariable("fmXmlEscape", new XmlEscape()); config.setSharedVariable("fmHtmlEscape", new HtmlEscape()); //config.setSharedVaribles(map); // 设置模板引擎,减少重复初始化消耗 this.setEngine(config); return config; }
Example 7
Source File: FreeMarkerConfigurationFactory.java From spring4-understanding with Apache License 2.0 | 4 votes |
/** * Prepare the FreeMarker Configuration and return it. * @return the FreeMarker Configuration object * @throws IOException if the config file wasn't found * @throws TemplateException on FreeMarker initialization failure */ public Configuration createConfiguration() throws IOException, TemplateException { Configuration config = newConfiguration(); Properties props = new Properties(); // Load config file if specified. if (this.configLocation != null) { if (logger.isInfoEnabled()) { logger.info("Loading FreeMarker configuration from " + this.configLocation); } PropertiesLoaderUtils.fillProperties(props, this.configLocation); } // Merge local properties if specified. if (this.freemarkerSettings != null) { props.putAll(this.freemarkerSettings); } // FreeMarker will only accept known keys in its setSettings and // setAllSharedVariables methods. if (!props.isEmpty()) { config.setSettings(props); } if (!CollectionUtils.isEmpty(this.freemarkerVariables)) { config.setAllSharedVariables(new SimpleHash(this.freemarkerVariables, config.getObjectWrapper())); } if (this.defaultEncoding != null) { config.setDefaultEncoding(this.defaultEncoding); } List<TemplateLoader> templateLoaders = new LinkedList<TemplateLoader>(this.templateLoaders); // Register template loaders that are supposed to kick in early. if (this.preTemplateLoaders != null) { templateLoaders.addAll(this.preTemplateLoaders); } // Register default template loaders. if (this.templateLoaderPaths != null) { for (String path : this.templateLoaderPaths) { templateLoaders.add(getTemplateLoaderForPath(path)); } } postProcessTemplateLoaders(templateLoaders); // Register template loaders that are supposed to kick in late. if (this.postTemplateLoaders != null) { templateLoaders.addAll(this.postTemplateLoaders); } TemplateLoader loader = getAggregateTemplateLoader(templateLoaders); if (loader != null) { config.setTemplateLoader(loader); } postProcessConfiguration(config); return config; }
Example 8
Source File: Freemarker.java From freeacs with MIT License | 2 votes |
/** * Sets the shared variables. * * @param config the new shared variables * @throws TemplateModelException the template model exception */ private static void setSharedVariables(Configuration config) throws TemplateModelException { config.setAllSharedVariables( new ResourceBundleModel(ResourceHandler.getProperties(), new BeansWrapper())); config.setSharedVariable("format", new ResourceMethod()); }