/** * A convenience constructor that allows for specifying {@link #setPrefix prefix} * and {@link #setSuffix suffix} as constructor arguments. * @param prefix the prefix that gets prepended to view names when building a URL * @param suffix the suffix that gets appended to view names when building a URL * @since 4.3 */ public FreeMarkerViewResolver(String prefix, String suffix) { this(); setPrefix(prefix); setSuffix(suffix); }
/** * Sets the default {@link #setViewClass view class} to {@link #requiredViewClass}: * by default {@link FreeMarkerView}. */ public FreeMarkerViewResolver() { setViewClass(requiredViewClass()); }
/** * Process the model map by merging it with the FreeMarker template. * Output is directed to the servlet response. * <p>This method can be overridden if custom behavior is needed. */ @Override protected void renderMergedTemplateModel( Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception { exposeHelpers(model, request); doRender(model, request, response); }
@Bean public FreeMarkerConfigurer freeMarkerConfigurer() { FreeMarkerConfigurer configurer = new FreeMarkerConfigurer(); configurer.setTemplateLoaderPath("/WEB-INF/"); return configurer; } }
@Bean public FreeMarkerViewResolver freeMarkerViewResolver() { return new FreeMarkerViewResolver("", ".ftl"); }
/** * Retrieve the FreeMarker template for the given locale, * to be rendering by this view. * <p>By default, the template specified by the "url" bean property * will be retrieved. * @param locale the current locale * @return the FreeMarker template to render * @throws IOException if the template file could not be retrieved * @see #setUrl * @see #getTemplate(String, java.util.Locale) */ protected Template getTemplate(Locale locale) throws IOException { String url = getUrl(); Assert.state(url != null, "'url' not set"); return getTemplate(url, locale); }
/** * Initialize FreeMarkerConfigurationFactory's Configuration * if not overridden by a preconfigured FreeMarker Configuration. * <p>Sets up a ClassTemplateLoader to use for loading Spring macros. * @see #createConfiguration * @see #setConfiguration */ @Override public void afterPropertiesSet() throws IOException, TemplateException { if (this.configuration == null) { this.configuration = createConfiguration(); } }
/** * Retrieve the FreeMarker template specified by the given name, * using the encoding specified by the "encoding" bean property. * <p>Can be called by subclasses to retrieve a specific template, * for example to render multiple templates into a single view. * @param name the file name of the desired template * @param locale the current locale * @return the FreeMarker template * @throws IOException if the template file could not be retrieved */ protected Template getTemplate(String name, Locale locale) throws IOException { return (getEncoding() != null ? obtainConfiguration().getTemplate(name, locale, getEncoding()) : obtainConfiguration().getTemplate(name, locale)); }
/** * Obtain the FreeMarker configuration for actual use. * @return the FreeMarker configuration (never {@code null}) * @throws IllegalStateException in case of no Configuration object set * @since 5.0 */ protected Configuration obtainConfiguration() { Configuration configuration = getConfiguration(); Assert.state(configuration != null, "No Configuration set"); return configuration; }
@Override @Nullable public String getServletName() { return FreeMarkerView.this.getBeanName(); }
@Test public void testForm2() throws Exception { assertEquals("<input type=\"text\" id=\"name\" name=\"name\" value=\"Darren\" class=\"myCssClass\" >", getMacroOutput("FORM2")); }
/** * Return the configured FreeMarker {@link ObjectWrapper}, or the * {@link ObjectWrapper#DEFAULT_WRAPPER default wrapper} if none specified. * @see freemarker.template.Configuration#getObjectWrapper() */ protected ObjectWrapper getObjectWrapper() { ObjectWrapper ow = obtainConfiguration().getObjectWrapper(); return (ow != null ? ow : new DefaultObjectWrapperBuilder(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS).build()); }
@Bean public FreeMarkerConfigurer freeMarkerConfigurer() { FreeMarkerConfigurer configurer = new FreeMarkerConfigurer(); configurer.setTemplateLoaderPath("/WEB-INF/"); return configurer; } }
public FreeMarkerRegistration() { super(new FreeMarkerViewResolver()); getViewResolver().setSuffix(".ftl"); } }
@Test public void testForm10() throws Exception { assertEquals("<input type=\"hidden\" id=\"name\" name=\"name\" value=\"Darren\" >", getMacroOutput("FORM10")); }
@Test public void testMessage() throws Exception { assertEquals("Howdy Mundo", getMacroOutput("MESSAGE")); }
@Test public void testThemeArgs() throws Exception { assertEquals("Howdy![World]", getMacroOutput("THEMEARGS")); }
@Test public void testUrlParams() throws Exception { assertEquals("/springtest/aftercontext/bar?spam=bucket", getMacroOutput("URLPARAMS")); }
@Test public void testForm3() throws Exception { assertEquals("<textarea id=\"name\" name=\"name\" >\nDarren</textarea>", getMacroOutput("FORM3")); }
@Test public void testDefaultTheme() throws Exception { assertEquals("hi! planet!", getMacroOutput("DEFAULTTHEME")); }