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

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

Introduction

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

Prototype

public void setConfiguration(Configuration configuration) 

Source Link

Document

Set a preconfigured Configuration to use for the FreeMarker web config, e.g.

Usage

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;/*from w w w  .j a v  a 2  s .com*/
}

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  a v a  2s . c  o m*/
}

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();//from  w  ww.  j a  v a2 s  . c  om

    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: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 .  ja v  a  2s  .  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();

    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.jav  a2s. 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());
}