Example usage for org.springframework.web.servlet.view.freemarker FreeMarkerViewResolver setApplicationContext

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

Introduction

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

Prototype

@Override
    public final void setApplicationContext(@Nullable ApplicationContext context) throws BeansException 

Source Link

Usage

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();/* w w  w  .  j ava2 s .c  o m*/

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