Example usage for org.springframework.web.servlet.view.freemarker FreeMarkerConfigurer FreeMarkerConfigurer

List of usage examples for org.springframework.web.servlet.view.freemarker FreeMarkerConfigurer FreeMarkerConfigurer

Introduction

In this page you can find the example usage for org.springframework.web.servlet.view.freemarker FreeMarkerConfigurer FreeMarkerConfigurer.

Prototype

FreeMarkerConfigurer

Source Link

Usage

From source file:org.hdiv.web.servlet.view.freemarker.FreeMarkerConfigurerTests.java

public void testTemplateLoaders() throws Exception {
    FreeMarkerConfigurer fc = new FreeMarkerConfigurer();
    fc.setTemplateLoaders(new TemplateLoader[] {});
    fc.afterPropertiesSet();//www .j  av a  2 s  .  c  o m
    assertTrue(fc.getConfiguration().getTemplateLoader() instanceof ClassTemplateLoader);

    fc = new FreeMarkerConfigurer();
    fc.setTemplateLoaders(new TemplateLoader[] { new ClassTemplateLoader() });
    fc.afterPropertiesSet();
    assertTrue(fc.getConfiguration().getTemplateLoader() instanceof MultiTemplateLoader);
}

From source file:com.usbank.wm.config.FreemarkerConfig.java

@Bean(name = "freemarkerConfig")
public FreeMarkerConfigurer freemarkerConfig() throws IOException {
    FreeMarkerConfigurer fc = new FreeMarkerConfigurer();
    fc.setConfiguration(freemarkerTemplateConfig());
    return fc;/* ww  w  .j  a  v  a 2 s  .c o m*/
}

From source file:com.gopivotal.cla.web.WebConfiguration.java

@Bean
FreeMarkerConfigurer freeMarkerConfigurer() {
    FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
    configurer.setTemplateLoaderPath("classpath:META-INF/freemarker");

    return configurer;
}

From source file:org.beast.project.template.config.FreemarkerConfig.java

@Bean(name = "freemarkerConfigurer")
public FreeMarkerConfigurer freemarkerConfigurer() throws IOException {
    FreeMarkerConfigurer fc = new FreeMarkerConfigurer();
    fc.setConfiguration(freemarkerTemplateConfig());
    return fc;//from   www. j  av a  2s.co  m
}

From source file:org.hdiv.web.servlet.view.freemarker.FreeMarkerMacroTests.java

public void setUp() throws Exception {
    wac = new StaticWebApplicationContext();
    wac.setServletContext(new MockServletContext());

    //final Template expectedTemplate = new Template();
    fc = new FreeMarkerConfigurer();
    fc.setPreferFileSystemAccess(false);
    fc.afterPropertiesSet();// www  . j  a  v a2s. com

    wac.getDefaultListableBeanFactory().registerSingleton("freeMarkerConfigurer", fc);
    wac.refresh();

    request = new MockHttpServletRequest();
    request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
    request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
    request.setAttribute(DispatcherServlet.THEME_RESOLVER_ATTRIBUTE, new FixedThemeResolver());
    response = new MockHttpServletResponse();
}

From source file:org.hdiv.web.servlet.view.freemarker.FreeMarkerViewTests.java

@Test
public void testValidTemplateName() throws Exception {
    FreeMarkerView fv = new FreeMarkerView();

    MockControl wmc = MockControl.createNiceControl(WebApplicationContext.class);
    WebApplicationContext wac = (WebApplicationContext) wmc.getMock();
    MockServletContext sc = new MockServletContext();

    wac.getBeansOfType(FreeMarkerConfig.class, true, false);
    Map configs = new HashMap();
    FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
    configurer.setConfiguration(new FreeMarkerTestConfiguration());
    configs.put("configurer", configurer);
    wmc.setReturnValue(configs);//from  w w  w.  j  a v a 2 s .  com
    wac.getParentBeanFactory();
    wmc.setReturnValue(null);
    wac.getServletContext();
    wmc.setReturnValue(sc, 5);
    wmc.replay();

    fv.setUrl("templateName");
    fv.setApplicationContext(wac);

    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addPreferredLocale(Locale.US);
    request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
    request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
    HttpServletResponse response = new MockHttpServletResponse();

    Map model = new HashMap();
    model.put("myattr", "myvalue");
    fv.render(model, request, response);

    wmc.verify();
    assertEquals(AbstractView.DEFAULT_CONTENT_TYPE, response.getContentType());
}

From source file:org.hdiv.web.servlet.view.freemarker.FreeMarkerViewTests.java

@Test
public void testKeepExistingContentType() throws Exception {
    FreeMarkerView fv = new FreeMarkerView();

    MockControl wmc = MockControl.createNiceControl(WebApplicationContext.class);
    WebApplicationContext wac = (WebApplicationContext) wmc.getMock();
    MockServletContext sc = new MockServletContext();

    wac.getBeansOfType(FreeMarkerConfig.class, true, false);
    Map configs = new HashMap();
    FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
    configurer.setConfiguration(new FreeMarkerTestConfiguration());
    configs.put("configurer", configurer);
    wmc.setReturnValue(configs);/*  w  w  w  .  j  a  v  a  2  s  . c o m*/
    wac.getParentBeanFactory();
    wmc.setReturnValue(null);
    wac.getServletContext();
    wmc.setReturnValue(sc, 5);
    wmc.replay();

    fv.setUrl("templateName");
    fv.setApplicationContext(wac);

    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addPreferredLocale(Locale.US);
    request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
    request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
    HttpServletResponse response = new MockHttpServletResponse();
    response.setContentType("myContentType");

    Map model = new HashMap();
    model.put("myattr", "myvalue");
    fv.render(model, request, response);

    wmc.verify();
    assertEquals("myContentType", response.getContentType());
}

From source file:org.hdiv.web.servlet.view.freemarker.FreeMarkerViewTests.java

@Test
public void testFreeMarkerViewResolver() throws Exception {
    FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
    configurer.setConfiguration(new FreeMarkerTestConfiguration());

    StaticWebApplicationContext wac = new StaticWebApplicationContext();
    wac.setServletContext(new MockServletContext());
    wac.getBeanFactory().registerSingleton("configurer", configurer);
    wac.refresh();/* ww w . ja v  a 2 s . com*/

    FreeMarkerViewResolver vr = new FreeMarkerViewResolver();
    vr.setPrefix("prefix_");
    vr.setSuffix("_suffix");
    vr.setApplicationContext(wac);

    View view = vr.resolveViewName("test", Locale.CANADA);
    assertEquals("Correct view class", FreeMarkerView.class, view.getClass());
    assertEquals("Correct URL", "prefix_test_suffix", ((FreeMarkerView) view).getUrl());

    view = vr.resolveViewName("non-existing", Locale.CANADA);
    assertNull(view);

    view = vr.resolveViewName("redirect:myUrl", Locale.getDefault());
    assertEquals("Correct view class", RedirectView.class, view.getClass());
    assertEquals("Correct URL", "myUrl", ((RedirectView) view).getUrl());

    view = vr.resolveViewName("forward:myUrl", Locale.getDefault());
    assertEquals("Correct view class", InternalResourceView.class, view.getClass());
    assertEquals("Correct URL", "myUrl", ((InternalResourceView) view).getUrl());
}

From source file:cn.newgxu.lab.core.config.SpringBeans.java

@Bean
public FreeMarkerConfig freeMarkerConfig() {
    FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
    configurer.setTemplateLoaderPath("/WEB-INF/views/");
    Properties settings = new Properties();
    settings.setProperty("template_update_delay", "0");
    settings.setProperty("default_encoding", "UTF-8");
    settings.setProperty("number_format", "0.##");
    settings.setProperty("datetime_format", "yyyy-MM-dd HH:mm:ss");
    settings.setProperty("classic_compatible", "true");
    settings.setProperty("template_exception_handler", "ignore");
    settings.setProperty("auto_import", "macro.ftl as L");
    configurer.setFreemarkerSettings(settings);
    return configurer;
}

From source file:org.springframework.cloud.netflix.hystrix.dashboard.HystrixDashboardConfiguration.java

/**
 * Overrides Spring Boot's {@link FreeMarkerAutoConfiguration} to prefer using a
 * {@link SpringTemplateLoader} instead of the file system. This corrects an issue
 * where Spring Boot may use an empty 'templates' file resource to resolve templates
 * instead of the packaged Hystrix classpath templates.
 * @return FreeMarker configuration/*from w w w  . jav a  2  s  .com*/
 */
@Bean
public FreeMarkerConfigurer freeMarkerConfigurer() {
    FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
    configurer.setTemplateLoaderPaths(DEFAULT_TEMPLATE_LOADER_PATH);
    configurer.setDefaultEncoding(DEFAULT_CHARSET);
    configurer.setPreferFileSystemAccess(false);
    return configurer;
}