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;
}
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);
}
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;
}
Aggregations