Java 类org.springframework.ui.freemarker.SpringTemplateLoader 实例源码

项目:onetwo    文件:FtlUtils.java   
public static TemplateLoader getTemplateLoaderForPath(ResourceLoader resourceLoader, String templateLoaderPath) {
    try {
        Resource path = resourceLoader.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 (IOException ex) {
        if (logger.isDebugEnabled()) {
            logger.debug("Cannot resolve template loader path [" + templateLoaderPath +
                    "] to [java.io.File]: using SpringTemplateLoader as fallback", ex);
        }
        return new SpringTemplateLoader(resourceLoader, templateLoaderPath);
    }

}
项目:spring4-understanding    文件:FreeMarkerConfigurerTests.java   
@Test
public void freeMarkerConfigurationFactoryBeanWithResourceLoaderPath() throws Exception {
    FreeMarkerConfigurationFactoryBean fcfb = new FreeMarkerConfigurationFactoryBean();
    fcfb.setTemplateLoaderPath("file:/mydir");
    fcfb.afterPropertiesSet();
    Configuration cfg = fcfb.getObject();
    assertTrue(cfg.getTemplateLoader() instanceof SpringTemplateLoader);
}
项目:spring4-understanding    文件:FreeMarkerConfigurerTests.java   
@Test  // SPR-12448
public void freeMarkerConfigurationAsBean() {
    DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
    RootBeanDefinition loaderDef = new RootBeanDefinition(SpringTemplateLoader.class);
    loaderDef.getConstructorArgumentValues().addGenericArgumentValue(new DefaultResourceLoader());
    loaderDef.getConstructorArgumentValues().addGenericArgumentValue("/freemarker");
    RootBeanDefinition configDef = new RootBeanDefinition(Configuration.class);
    configDef.getPropertyValues().add("templateLoader", loaderDef);
    beanFactory.registerBeanDefinition("freeMarkerConfig", configDef);
    beanFactory.getBean(Configuration.class);
}
项目:leopard    文件:ClassPathModelAndView.java   
@Override
public void render(Map<String, ?> model, HttpServletRequest request, HttpServletResponse response) throws Exception {
    Configuration config = configurer.getConfiguration();
    // config.setTemplateLoader(new ClassTemplateLoader(this.getClass(), folder));
    config.setTemplateLoader(new SpringTemplateLoader(new DefaultResourceLoader(), folder));
    // return config.getTemplate(name, "UTF-8");

    Template template = config.getTemplate(viewName + ".ftl", "UTF-8");
    response.setContentType(getContentType());
    Writer out = response.getWriter();
    template.process(model, out);
}
项目:uaa-service    文件:TemplateConfig.java   
private TemplateLoader getTemplateLoader(final String templatePath) {
    try {
        // Classpath loading
        if (templatePath.startsWith("classpath:")) {
            final Resource path = resourceLoader.getResource(templatePath);
            return new FileTemplateLoader(path.getFile());
        }

        // Filebase loading
        return new FileTemplateLoader(new File(templatePath));
    } catch (final IOException e) {
        LOGGER.debug("Error while getting templateLoader, will fallback to SpringTemplateLoader.", e);
        return new SpringTemplateLoader(resourceLoader, templatePath);
    }
}
项目:class-guard    文件:FreeMarkerConfigurerTests.java   
public void testFreeMarkerConfigurationFactoryBeanWithResourceLoaderPath() throws Exception {
    FreeMarkerConfigurationFactoryBean fcfb = new FreeMarkerConfigurationFactoryBean();
    fcfb.setTemplateLoaderPath("file:/mydir");
    fcfb.afterPropertiesSet();
    Configuration cfg = fcfb.getObject();
    assertTrue(cfg.getTemplateLoader() instanceof SpringTemplateLoader);
}
项目:spring5demo    文件:ViewConfig.java   
@Bean
public FreeMarkerConfigurer freeMarkerConfig(ApplicationContext context) {
    final FreeMarkerConfigurer freeMarkerConfigurer = new FreeMarkerConfigurer();
    freeMarkerConfigurer.setPreTemplateLoaders(new SpringTemplateLoader(context, "/templates/"));
    return freeMarkerConfigurer;
}