Example usage for org.springframework.web.servlet.view.freemarker FreeMarkerView setUrl

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

Introduction

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

Prototype

public void setUrl(@Nullable String url) 

Source Link

Document

Set the URL of the resource that this view wraps.

Usage

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

public void testAllMacros() throws Exception {
    DummyMacroRequestContext rc = new DummyMacroRequestContext(request);
    Map msgMap = new HashMap();
    msgMap.put("hello", "Howdy");
    msgMap.put("world", "Mundo");
    rc.setMessageMap(msgMap);/*from   ww w  . ja  v  a  2  s  . c om*/
    Map themeMsgMap = new HashMap();
    themeMsgMap.put("hello", "Howdy!");
    themeMsgMap.put("world", "Mundo!");
    rc.setThemeMessageMap(themeMsgMap);
    rc.setContextPath("/springtest");

    TestBean tb = new TestBean("Darren", 99);
    tb.setSpouse(new TestBean("Fred"));
    tb.setJedi(true);
    request.setAttribute("command", tb);

    HashMap names = new HashMap();
    names.put("Darren", "Darren Davison");
    names.put("John", "John Doe");
    names.put("Fred", "Fred Bloggs");
    names.put("Rob&Harrop", "Rob Harrop");

    Configuration config = fc.getConfiguration();
    Map model = new HashMap();
    model.put("command", tb);
    model.put("springMacroRequestContext", rc);
    model.put("msgArgs", new Object[] { "World" });
    model.put("nameOptionMap", names);
    model.put("options", names.values());

    FreeMarkerView view = new FreeMarkerView();
    view.setBeanName("myView");
    view.setUrl("test.ftl");
    view.setExposeSpringMacroHelpers(false);
    view.setConfiguration(config);
    view.setServletContext(new MockServletContext());

    view.render(model, request, response);

    // tokenize output and ignore whitespace
    String output = response.getContentAsString();
    System.out.println(output);
    String[] tokens = StringUtils.tokenizeToStringArray(output, "\t\n");

    for (int i = 0; i < tokens.length; i++) {
        if (tokens[i].equals("NAME"))
            assertEquals("Darren", tokens[i + 1]);
        if (tokens[i].equals("AGE"))
            assertEquals("99", tokens[i + 1]);
        if (tokens[i].equals("MESSAGE"))
            assertEquals("Howdy Mundo", tokens[i + 1]);
        if (tokens[i].equals("DEFAULTMESSAGE"))
            assertEquals("hi planet", tokens[i + 1]);
        if (tokens[i].equals("MESSAGEARGS"))
            assertEquals("Howdy[World]", tokens[i + 1]);
        if (tokens[i].equals("MESSAGEARGSWITHDEFAULTMESSAGE"))
            assertEquals("Hi", tokens[i + 1]);
        if (tokens[i].equals("THEME"))
            assertEquals("Howdy! Mundo!", tokens[i + 1]);
        if (tokens[i].equals("DEFAULTTHEME"))
            assertEquals("hi! planet!", tokens[i + 1]);
        if (tokens[i].equals("THEMEARGS"))
            assertEquals("Howdy![World]", tokens[i + 1]);
        if (tokens[i].equals("THEMEARGSWITHDEFAULTMESSAGE"))
            assertEquals("Hi!", tokens[i + 1]);
        if (tokens[i].equals("URL"))
            assertEquals("/springtest/aftercontext.html", tokens[i + 1]);
        if (tokens[i].equals("FORM1"))
            assertEquals("<input type=\"text\" id=\"name\" name=\"name\" value=\"Darren\"     >",
                    tokens[i + 1]);
        if (tokens[i].equals("FORM2"))
            assertEquals(
                    "<input type=\"text\" id=\"name\" name=\"name\" value=\"Darren\" class=\"myCssClass\"    >",
                    tokens[i + 1]);
        if (tokens[i].equals("FORM3"))
            assertEquals("<textarea id=\"name\" name=\"name\" >Darren</textarea>", tokens[i + 1]);
        if (tokens[i].equals("FORM4"))
            assertEquals("<textarea id=\"name\" name=\"name\" rows=10 cols=30>Darren</textarea>",
                    tokens[i + 1]);
        //TODO verify remaining output (fix whitespace)
        if (tokens[i].equals("FORM9"))
            assertEquals("<input type=\"password\" id=\"name\" name=\"name\" value=\"\"     >", tokens[i + 1]);
        if (tokens[i].equals("FORM10"))
            assertEquals("<input type=\"hidden\" id=\"name\" name=\"name\" value=\"Darren\"     >",
                    tokens[i + 1]);
        if (tokens[i].equals("FORM11"))
            assertEquals("<input type=\"text\" id=\"name\" name=\"name\" value=\"Darren\"     >",
                    tokens[i + 1]);
        if (tokens[i].equals("FORM12"))
            assertEquals("<input type=\"hidden\" id=\"name\" name=\"name\" value=\"Darren\"     >",
                    tokens[i + 1]);
        if (tokens[i].equals("FORM13"))
            assertEquals("<input type=\"password\" id=\"name\" name=\"name\" value=\"\"     >", tokens[i + 1]);
        if (tokens[i].equals("FORM15"))
            assertEquals("<input type=\"hidden\" name=\"_name\" value=\"on\"/>", tokens[i + 1]);
        if (tokens[i].equals("FORM15"))
            assertEquals("<input type=\"checkbox\" id=\"name\" name=\"name\" />", tokens[i + 2]);
        if (tokens[i].equals("FORM16"))
            assertEquals("<input type=\"hidden\" name=\"_jedi\" value=\"on\"/>", tokens[i + 1]);
        if (tokens[i].equals("FORM16"))
            assertEquals("<input type=\"checkbox\" id=\"jedi\" name=\"jedi\" checked=\"checked\" />",
                    tokens[i + 2]);
    }
}

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

public void testSpringMacroRequestContextAttributeUsed() {
    final String helperTool = "wrongType";

    FreeMarkerView fv = new FreeMarkerView() {
        @Override/*from w  w w  .  j  a v  a  2s.  co  m*/
        protected void processTemplate(Template template, SimpleHash model, HttpServletResponse response) {
            fail();
        }
    };
    fv.setUrl(TEMPLATE_FILE);
    fv.setApplicationContext(wac);
    fv.setExposeSpringMacroHelpers(true);

    Map model = new HashMap();
    model.put(FreeMarkerView.SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE, helperTool);

    try {
        fv.render(model, request, response);
    } catch (Exception ex) {
        assertTrue(ex instanceof ServletException);
        assertTrue(ex.getMessage().contains(FreeMarkerView.SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE));
    }
}

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

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

    MockControl wmc = MockControl.createControl(WebApplicationContext.class);
    WebApplicationContext wac = (WebApplicationContext) wmc.getMock();
    wac.getBeansOfType(FreeMarkerConfig.class, true, false);
    wmc.setReturnValue(new HashMap());
    wac.getParentBeanFactory();/*from  w  w w. ja va 2 s  .c  o m*/
    wmc.setReturnValue(null);
    wac.getServletContext();
    wmc.setReturnValue(new MockServletContext());
    wmc.replay();

    fv.setUrl("anythingButNull");
    try {
        fv.setApplicationContext(wac);
        fv.afterPropertiesSet();
        fail("Should have thrown BeanDefinitionStoreException");
    } catch (ApplicationContextException ex) {
        // Check there's a helpful error message
        assertTrue(ex.getMessage().indexOf("FreeMarkerConfig") != -1);
    }

    wmc.verify();
}

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  ww.  java2s  .co 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 ww. j  av  a2 s  .c om
    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.FreeMarkerMacroTests.java

public void testExposeSpringMacroHelpers() throws Exception {
    FreeMarkerView fv = new FreeMarkerView() {
        @Override//w  w  w  .j a  v a  2 s . com
        protected void processTemplate(Template template, SimpleHash fmModel, HttpServletResponse response)
                throws TemplateException {
            Map model = fmModel.toMap();
            assertTrue(
                    model.get(FreeMarkerView.SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE) instanceof RequestContext);
            RequestContext rc = (RequestContext) model
                    .get(FreeMarkerView.SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE);
            BindStatus status = rc.getBindStatus("tb.name");
            assertEquals("name", status.getExpression());
            assertEquals("juergen", status.getValue());
        }
    };
    fv.setUrl(TEMPLATE_FILE);
    fv.setApplicationContext(wac);
    fv.setExposeSpringMacroHelpers(true);

    Map model = new HashMap();
    model.put("tb", new TestBean("juergen", 99));
    fv.render(model, request, response);
}