Java Code Examples for org.springframework.ui.freemarker.FreeMarkerConfigurationFactory

Following code examples demonstrate how to use org.springframework.ui.freemarker.FreeMarkerConfigurationFactoryfrom spring-framework. These examples are extracted from various highly rated open source projects. You can directly use these code snippets or view their entire linked source code. These snippets are extracted to provide contextual information about how to use this class in the real world. These samples also let you understand some good practices on how to use org.springframework.ui.freemarker.FreeMarkerConfigurationFactoryand various code implementation of this class.

	public static FreemarkerParser getInstance() {
		if (freemarkerParser == null) {
			try {
				freemarkerParser = new FreemarkerParser();
			} catch (IOException e) {
				log.warn("Al crear la singleton de configuraciĆ³n de freemarkerParser", e);
			} catch (TemplateException e) {
				log.warn("Al crear la singleton de configuraciĆ³n de freemarkerParser", e);
			}
		}
		return freemarkerParser;
	} 


    public FreeMarkerConfigurer freemarkerConfig() throws IOException, TemplateException {
        FreeMarkerConfigurationFactory factory = new FreeMarkerConfigurationFactory();
        factory.setTemplateLoaderPaths("classpath:templates", "src/main/resource/templates");
        factory.setDefaultEncoding("UTF-8");
        FreeMarkerConfigurer result = new FreeMarkerConfigurer();
        result.setConfiguration(factory.createConfiguration());
        return result;
    } 

    public FreeMarkerConfigurationFactory freeMarkerConfigurationFactory() {
        FreeMarkerConfigurationFactory freeMarkerConfigurationFactory = new FreeMarkerConfigurationFactory();
        freeMarkerConfigurationFactory.setTemplateLoaderPath("email-template");
        return freeMarkerConfigurationFactory;
    } 

    public FreeMarkerConfigurer freemarkerConfig() throws IOException, TemplateException {
        FreeMarkerConfigurationFactory factory = new FreeMarkerConfigurationFactory();
        factory.setTemplateLoaderPaths("classpath:templates");
        factory.setDefaultEncoding("UTF-8");
        FreeMarkerConfigurer result = new FreeMarkerConfigurer();
        result.setConfiguration(factory.createConfiguration());
        return result;
    } 

	protected void applyProperties(FreeMarkerConfigurationFactory factory) {
		factory.setTemplateLoaderPaths(this.properties.getTemplateLoaderPath());
		factory.setPreferFileSystemAccess(this.properties.isPreferFileSystemAccess());
		factory.setDefaultEncoding(this.properties.getCharsetName());
		Properties settings = new Properties();
		settings.putAll(this.properties.getSettings());
		factory.setFreemarkerSettings(settings);
	} 

    @Bean
    public FreeMarkerConfigurer freeMarkerConfigurer(ImageMethod image, CssMethod cssMethod, JsMethod jsMethod)
            throws IOException, TemplateException {
        FreeMarkerConfigurationFactory factory = new FreeMarkerConfigurationFactory();
        factory.setTemplateLoaderPaths("classpath:templates", "src/main/resource/templates");
        factory.setDefaultEncoding("UTF-8");
        FreeMarkerConfigurer result = new FreeMarkerConfigurer();
        freemarker.template.Configuration configuration = factory.createConfiguration();
        result.setConfiguration(configuration);

        configuration.setSharedVariable("image", image);
        configuration.setSharedVariable("css", cssMethod);
        configuration.setSharedVariable("js", jsMethod);
        return result;
    } 

    public FreeMarkerConfigurer freemarkerConfig() throws IOException, TemplateException {
        FreeMarkerConfigurationFactory factory = new FreeMarkerConfigurationFactory();
        factory.setDefaultEncoding("UTF-8");
        factory.setTemplateLoaderPaths("classpath:templates", "src/main/resources/templates");
        FreeMarkerConfigurer result = new FreeMarkerConfigurer();
        freemarker.template.Configuration cfg = factory.createConfiguration();
        result.setConfiguration(cfg);
        return result;
    } 

    public FreeMarkerConfigurer freemarkerConfig() throws IOException, TemplateException {
        FreeMarkerConfigurationFactory factory = new VelocityConfigurerFactory();
        factory.setTemplateLoaderPaths("classpath:templates", "src/main/resource/templates");
        factory.setDefaultEncoding("UTF-8");
        FreeMarkerConfigurer result = new FreeMarkerConfigurer();
        result.setConfiguration(factory.createConfiguration());
        return result;
    } 

Advertisement
Javadoc
Factory that configures a FreeMarker Configuration. Can be used standalone, but typically you will e

ither use FreeMarkerConfigurationFactoryBean for preparing a Configuration as bean reference, or FreeMarkerConfigurer for web views.

The optional "configLocation" property sets the location of a FreeMarker properties file, within the current application. FreeMarker properties can be overridden via "freemarkerSettings". All of these properties will be set by calling FreeMarker's Configuration.setSettings() method and are subject to constraints set by FreeMarker.

The "freemarkerVariables" property can be used to specify a Map of shared variables that will be applied to the Configuration via the setAllSharedVariables() method. Like setSettings(), these entries are subject to FreeMarker constraints.

The simplest way to use this class is to specify a "templateLoaderPath"; FreeMarker does not need any further configuration then.

Note: Spring's FreeMarker support requires FreeMarker 2.3 or higher. @author Darren Davison @author Juergen Hoeller @since 03.03.2004 @see #setConfigLocation @see #setFreemarkerSettings @see #setFreemarkerVariables @see #setTemplateLoaderPath @see #createConfiguration @see FreeMarkerConfigurationFactoryBean @see org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer @see freemarker.template.Configuration

Read More
Advertisement