Example usage for org.springframework.ui.freemarker SpringTemplateLoader SpringTemplateLoader

List of usage examples for org.springframework.ui.freemarker SpringTemplateLoader SpringTemplateLoader

Introduction

In this page you can find the example usage for org.springframework.ui.freemarker SpringTemplateLoader SpringTemplateLoader.

Prototype

public SpringTemplateLoader(ResourceLoader resourceLoader, String templateLoaderPath) 

Source Link

Document

Create a new SpringTemplateLoader.

Usage

From source file:com.surveypanel.view.FreemarkerManager.java

/**
 * Create the instance of the freemarker Configuration object.
 * <p/>/*www .  ja  va2s  . co m*/
 * this implementation
 * <ul>
 * <li>obtains the default configuration from Configuration.getDefaultConfiguration()
 * <li>sets up template loading from a ClassTemplateLoader and a WebappTemplateLoader
 * <li>sets up the object wrapper to be the BeansWrapper
 * <li>loads settings from the classpath file /freemarker.properties
 * </ul>
 * @param form 
 *
 * @param servletContext
 */
protected static Configuration createConfiguration(Form form) throws TemplateException {
    Configuration configuration = new Configuration();
    configuration.setTemplateLoader(new MultiTemplateLoader(new TemplateLoader[] { form.getTemplateLoader(),
            new SpringTemplateLoader(new DefaultResourceLoader(), "") }));
    configuration.setTemplateExceptionHandler(TemplateExceptionHandler.HTML_DEBUG_HANDLER);
    if (mruMaxStrongSize > 0) {
        configuration.setSetting(freemarker.template.Configuration.CACHE_STORAGE_KEY,
                "strong:" + mruMaxStrongSize);
    }
    if (encoding != null) {
        configuration.setDefaultEncoding(encoding);
    }
    loadSettings(configuration);

    configuration.setWhitespaceStripping(true);
    configuration.setSharedVariable("a", new Anchor());
    configuration.setSharedVariable("input", new Input());
    configuration.setSharedVariable("text", new I18nTag(form.getQuestionnaire()));

    return configuration;
}

From source file:org.springframework.ui.freemarker.FreeMarkerConfigurationFactory.java

/**
 * Determine a FreeMarker TemplateLoader for the given path.
 * <p>Default implementation creates either a FileTemplateLoader or
 * a SpringTemplateLoader.//from   w w w. jav a  2 s.  c om
 * @param templateLoaderPath the path to load templates from
 * @return an appropriate TemplateLoader
 * @see freemarker.cache.FileTemplateLoader
 * @see SpringTemplateLoader
 */
protected TemplateLoader getTemplateLoaderForPath(String templateLoaderPath) {
    if (isPreferFileSystemAccess()) {
        // Try to load via the file system, fall back to SpringTemplateLoader
        // (for hot detection of template changes, if possible).
        try {
            Resource path = getResourceLoader().getResource(templateLoaderPath);
            File file = path.getFile(); // will fail if not resolvable in the file system
            if (logger.isDebugEnabled()) {
                logger.debug("Template loader path [" + path + "] resolved to file path ["
                        + file.getAbsolutePath() + "]");
            }
            return new FileTemplateLoader(file);
        } catch (Exception ex) {
            if (logger.isDebugEnabled()) {
                logger.debug("Cannot resolve template loader path [" + templateLoaderPath
                        + "] to [java.io.File]: using SpringTemplateLoader as fallback", ex);
            }
            return new SpringTemplateLoader(getResourceLoader(), templateLoaderPath);
        }
    } else {
        // Always load via SpringTemplateLoader (without hot detection of template changes).
        logger.debug("File system access not preferred: using SpringTemplateLoader");
        return new SpringTemplateLoader(getResourceLoader(), templateLoaderPath);
    }
}