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); } }
@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); }
@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); }
@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); }
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); } }
public void testFreeMarkerConfigurationFactoryBeanWithResourceLoaderPath() throws Exception { FreeMarkerConfigurationFactoryBean fcfb = new FreeMarkerConfigurationFactoryBean(); fcfb.setTemplateLoaderPath("file:/mydir"); fcfb.afterPropertiesSet(); Configuration cfg = fcfb.getObject(); assertTrue(cfg.getTemplateLoader() instanceof SpringTemplateLoader); }
@Bean public FreeMarkerConfigurer freeMarkerConfig(ApplicationContext context) { final FreeMarkerConfigurer freeMarkerConfigurer = new FreeMarkerConfigurer(); freeMarkerConfigurer.setPreTemplateLoaders(new SpringTemplateLoader(context, "/templates/")); return freeMarkerConfigurer; }