Search in sources :

Example 16 with FreeMarkerConfigurer

use of org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer in project tutorials by eugenp.

the class FreemarkerConfiguration method freemarkerConfig.

@Bean
public FreeMarkerConfigurer freemarkerConfig() {
    FreeMarkerConfigurer freeMarkerConfigurer = new FreeMarkerConfigurer();
    freeMarkerConfigurer.setTemplateLoaderPath("/WEB-INF/views/");
    return freeMarkerConfigurer;
}
Also used : FreeMarkerConfigurer(org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer) Bean(org.springframework.context.annotation.Bean)

Example 17 with FreeMarkerConfigurer

use of org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer in project spring-framework by spring-projects.

the class MvcNamespaceTests method testViewResolution.

@Test
public void testViewResolution() throws Exception {
    loadBeanDefinitions("mvc-config-view-resolution.xml");
    ViewResolverComposite compositeResolver = this.appContext.getBean(ViewResolverComposite.class);
    assertThat(compositeResolver).isNotNull();
    assertThat(compositeResolver.getViewResolvers().size()).as("Actual: " + compositeResolver.getViewResolvers()).isEqualTo(7);
    assertThat(compositeResolver.getOrder()).isEqualTo(Ordered.LOWEST_PRECEDENCE);
    List<ViewResolver> resolvers = compositeResolver.getViewResolvers();
    assertThat(resolvers.get(0).getClass()).isEqualTo(BeanNameViewResolver.class);
    ViewResolver resolver = resolvers.get(1);
    assertThat(resolver.getClass()).isEqualTo(InternalResourceViewResolver.class);
    DirectFieldAccessor accessor = new DirectFieldAccessor(resolver);
    assertThat(accessor.getPropertyValue("viewClass")).isEqualTo(InternalResourceView.class);
    resolver = resolvers.get(2);
    assertThat(resolver).isInstanceOf(FreeMarkerViewResolver.class);
    accessor = new DirectFieldAccessor(resolver);
    assertThat(accessor.getPropertyValue("prefix")).isEqualTo("freemarker-");
    assertThat(accessor.getPropertyValue("suffix")).isEqualTo(".freemarker");
    assertThat((String[]) accessor.getPropertyValue("viewNames")).isEqualTo(new String[] { "my*", "*Report" });
    assertThat(accessor.getPropertyValue("cacheLimit")).isEqualTo(1024);
    resolver = resolvers.get(3);
    assertThat(resolver).isInstanceOf(GroovyMarkupViewResolver.class);
    accessor = new DirectFieldAccessor(resolver);
    assertThat(accessor.getPropertyValue("prefix")).isEqualTo("");
    assertThat(accessor.getPropertyValue("suffix")).isEqualTo(".tpl");
    assertThat(accessor.getPropertyValue("cacheLimit")).isEqualTo(1024);
    resolver = resolvers.get(4);
    assertThat(resolver).isInstanceOf(ScriptTemplateViewResolver.class);
    accessor = new DirectFieldAccessor(resolver);
    assertThat(accessor.getPropertyValue("prefix")).isEqualTo("");
    assertThat(accessor.getPropertyValue("suffix")).isEqualTo("");
    assertThat(accessor.getPropertyValue("cacheLimit")).isEqualTo(1024);
    assertThat(resolvers.get(5).getClass()).isEqualTo(InternalResourceViewResolver.class);
    assertThat(resolvers.get(6).getClass()).isEqualTo(InternalResourceViewResolver.class);
    FreeMarkerConfigurer freeMarkerConfigurer = appContext.getBean(FreeMarkerConfigurer.class);
    assertThat(freeMarkerConfigurer).isNotNull();
    accessor = new DirectFieldAccessor(freeMarkerConfigurer);
    assertThat((String[]) accessor.getPropertyValue("templateLoaderPaths")).isEqualTo(new String[] { "/", "/test" });
    GroovyMarkupConfigurer groovyMarkupConfigurer = appContext.getBean(GroovyMarkupConfigurer.class);
    assertThat(groovyMarkupConfigurer).isNotNull();
    assertThat(groovyMarkupConfigurer.getResourceLoaderPath()).isEqualTo("/test");
    assertThat(groovyMarkupConfigurer.isAutoIndent()).isTrue();
    assertThat(groovyMarkupConfigurer.isCacheTemplates()).isFalse();
    ScriptTemplateConfigurer scriptTemplateConfigurer = appContext.getBean(ScriptTemplateConfigurer.class);
    assertThat(scriptTemplateConfigurer).isNotNull();
    assertThat(scriptTemplateConfigurer.getRenderFunction()).isEqualTo("render");
    assertThat(scriptTemplateConfigurer.getContentType()).isEqualTo(MediaType.TEXT_PLAIN_VALUE);
    assertThat(scriptTemplateConfigurer.getCharset()).isEqualTo(StandardCharsets.ISO_8859_1);
    assertThat(scriptTemplateConfigurer.getResourceLoaderPath()).isEqualTo("classpath:");
    assertThat(scriptTemplateConfigurer.isSharedEngine()).isFalse();
    String[] scripts = { "org/springframework/web/servlet/view/script/nashorn/render.js" };
    accessor = new DirectFieldAccessor(scriptTemplateConfigurer);
    assertThat((String[]) accessor.getPropertyValue("scripts")).isEqualTo(scripts);
}
Also used : GroovyMarkupConfigurer(org.springframework.web.servlet.view.groovy.GroovyMarkupConfigurer) ViewResolverComposite(org.springframework.web.servlet.view.ViewResolverComposite) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) ScriptTemplateConfigurer(org.springframework.web.servlet.view.script.ScriptTemplateConfigurer) ContentNegotiatingViewResolver(org.springframework.web.servlet.view.ContentNegotiatingViewResolver) ScriptTemplateViewResolver(org.springframework.web.servlet.view.script.ScriptTemplateViewResolver) FreeMarkerViewResolver(org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver) ViewResolver(org.springframework.web.servlet.ViewResolver) BeanNameViewResolver(org.springframework.web.servlet.view.BeanNameViewResolver) GroovyMarkupViewResolver(org.springframework.web.servlet.view.groovy.GroovyMarkupViewResolver) InternalResourceViewResolver(org.springframework.web.servlet.view.InternalResourceViewResolver) FreeMarkerConfigurer(org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer) Test(org.junit.jupiter.api.Test)

Example 18 with FreeMarkerConfigurer

use of org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer in project wombat by PLOS.

the class SpringConfiguration method freeMarkerConfig.

@Bean
public FreeMarkerConfig freeMarkerConfig(ServletContext servletContext, SiteSet siteSet, IsDevFeatureEnabledDirective isDevFeatureEnabledDirective, SiteLinkDirective siteLinkDirective, RenderCssLinksDirective renderCssLinksDirective, RenderJsDirective renderJsDirective, BuildInfoDirective buildInfoDirective, FetchHtmlDirective fetchHtmlDirective, ThemeConfigDirective themeConfigDirective, AppLinkDirective appLinkDirective, ArticleExcerptTransformDirective articleExcerptTransformDirective, GlobalConfigDirective globalConfigDirective, RuntimeConfiguration runtimeConfiguration) throws IOException {
    SiteTemplateLoader loader = new SiteTemplateLoader(servletContext, siteSet);
    FreeMarkerConfigurer config = new FreeMarkerConfigurer();
    config.setPreTemplateLoaders(loader);
    // Freemarker custom directives used throughout the app.
    // TODO: should all of these be their own @Beans?  I'm only doing that now for
    // ones that have dependencies on spring-injection.
    ImmutableMap.Builder<String, Object> variables = ImmutableMap.builder();
    variables.put("pluralize", new PluralizeDirective());
    variables.put("formatJsonDate", new Iso8601DateDirective());
    variables.put("replaceParams", new ReplaceParametersDirective());
    variables.put("siteLink", siteLinkDirective);
    variables.put("isDevFeatureEnabled", isDevFeatureEnabledDirective);
    variables.put("cssLink", new CssLinkDirective());
    variables.put("renderCssLinks", renderCssLinksDirective);
    variables.put("js", new JsDirective());
    variables.put("renderJs", renderJsDirective);
    variables.put("buildInfo", buildInfoDirective);
    variables.put("fetchHtml", fetchHtmlDirective);
    variables.put("themeConfig", themeConfigDirective);
    variables.put("appLink", appLinkDirective);
    variables.put("abbreviatedName", new AbbreviatedNameDirective());
    variables.put("xform", articleExcerptTransformDirective);
    variables.put("globalConfig", globalConfigDirective);
    variables.put("collectionsUrl", runtimeConfiguration.getCollectionsUrl());
    config.setFreemarkerVariables(variables.build());
    return config;
}
Also used : AbbreviatedNameDirective(org.ambraproject.wombat.freemarker.AbbreviatedNameDirective) SiteTemplateLoader(org.ambraproject.wombat.config.site.SiteTemplateLoader) CssLinkDirective(org.ambraproject.wombat.freemarker.asset.CssLinkDirective) PluralizeDirective(org.ambraproject.wombat.freemarker.PluralizeDirective) ReplaceParametersDirective(org.ambraproject.wombat.freemarker.ReplaceParametersDirective) Iso8601DateDirective(org.ambraproject.wombat.freemarker.Iso8601DateDirective) JsDirective(org.ambraproject.wombat.freemarker.asset.JsDirective) RenderJsDirective(org.ambraproject.wombat.freemarker.asset.RenderJsDirective) FreeMarkerConfigurer(org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer) ImmutableMap(com.google.common.collect.ImmutableMap) Bean(org.springframework.context.annotation.Bean)

Aggregations

FreeMarkerConfigurer (org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer)18 Bean (org.springframework.context.annotation.Bean)13 Properties (java.util.Properties)4 ImmutableMap (com.google.common.collect.ImmutableMap)3 Iso8601DateDirective (org.ambraproject.wombat.freemarker.Iso8601DateDirective)3 ReplaceParametersDirective (org.ambraproject.wombat.freemarker.ReplaceParametersDirective)3 StringWriter (java.io.StringWriter)2 HashMap (java.util.HashMap)2 SiteTemplateLoader (org.ambraproject.wombat.config.site.SiteTemplateLoader)2 AbbreviatedNameDirective (org.ambraproject.wombat.freemarker.AbbreviatedNameDirective)2 CssLinkDirective (org.ambraproject.wombat.freemarker.asset.CssLinkDirective)2 JsDirective (org.ambraproject.wombat.freemarker.asset.JsDirective)2 RenderJsDirective (org.ambraproject.wombat.freemarker.asset.RenderJsDirective)2 Test (org.junit.jupiter.api.Test)2 FileTemplateLoader (freemarker.cache.FileTemplateLoader)1 MultiTemplateLoader (freemarker.cache.MultiTemplateLoader)1 TemplateLoader (freemarker.cache.TemplateLoader)1 BeansWrapper (freemarker.ext.beans.BeansWrapper)1 Configuration (freemarker.template.Configuration)1 TemplateException (freemarker.template.TemplateException)1