/** * Convenience constructor with a prefix and suffix. * @param suffix the suffix to prepend view names with * @param prefix the prefix to prepend view names with */ public FreeMarkerViewResolver(String prefix, String suffix) { setViewClass(requiredViewClass()); setPrefix(prefix); setSuffix(suffix); }
/** * Simple constructor. */ public FreeMarkerViewResolver() { setViewClass(requiredViewClass()); }
public FreeMarkerConfigurer() { setDefaultEncoding("UTF-8"); }
@Before public void setup() throws Exception { this.context = new GenericApplicationContext(); this.context.refresh(); FreeMarkerConfigurer configurer = new FreeMarkerConfigurer(); configurer.setPreferFileSystemAccess(false); configurer.setTemplateLoaderPath(TEMPLATE_PATH); configurer.setResourceLoader(this.context); this.freeMarkerConfig = configurer.createConfiguration(); }
@Bean public FreeMarkerConfigurer freeMarkerConfig() { FreeMarkerConfigurer configurer = new FreeMarkerConfigurer(); configurer.setPreferFileSystemAccess(false); configurer.setTemplateLoaderPath("classpath*:org/springframework/web/reactive/view/freemarker/"); return configurer; } }
@Override public void afterPropertiesSet() throws Exception { super.afterPropertiesSet(); if (getConfiguration() == null) { FreeMarkerConfig config = autodetectConfiguration(); setConfiguration(config.getConfiguration()); } }
@Test public void render() throws Exception { FreeMarkerView view = new FreeMarkerView(); view.setConfiguration(this.freeMarkerConfig); view.setUrl("test.ftl"); ModelMap model = new ExtendedModelMap(); model.addAttribute("hello", "hi FreeMarker"); view.render(model, null, this.exchange).block(Duration.ofMillis(5000)); StepVerifier.create(this.exchange.getResponse().getBody()) .consumeNextWith(buf -> assertEquals("<html><body>hi FreeMarker</body></html>", asString(buf))) .expectComplete() .verify(); }
@Test public void checkResourceExists() throws Exception { FreeMarkerView view = new FreeMarkerView(); view.setConfiguration(this.freeMarkerConfig); view.setUrl("test.ftl"); assertTrue(view.checkResourceExists(Locale.US)); }
/** * 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 */ protected Template getTemplate(Locale locale) throws IOException { return (getEncoding() != null ? obtainConfiguration().getTemplate(getUrl(), locale, getEncoding()) : obtainConfiguration().getTemplate(getUrl(), locale)); }
/** * Check that the FreeMarker template used for this view exists and is valid. * <p>Can be overridden to customize the behavior, for example in case of * multiple templates to be rendered into a single view. */ @Override public boolean checkResourceExists(Locale locale) throws Exception { try { // Check that we can get the template, even if we might subsequently get it again. getTemplate(locale); return true; } catch (FileNotFoundException ex) { // Allow for ViewResolver chaining... return false; } catch (ParseException ex) { throw new ApplicationContextException( "Failed to parse FreeMarker template for URL [" + getUrl() + "]", ex); } catch (IOException ex) { throw new ApplicationContextException( "Could not load FreeMarker template for URL [" + getUrl() + "]", ex); } }
@Bean public FreeMarkerConfigurer freeMarkerConfig() { return new FreeMarkerConfigurer(); } }
@Test public void noFreeMarkerConfig() throws Exception { this.exception.expect(ApplicationContextException.class); this.exception.expectMessage("Must define a single FreeMarkerConfig bean"); FreeMarkerView view = new FreeMarkerView(); view.setApplicationContext(this.context); view.setUrl("anythingButNull"); view.afterPropertiesSet(); }
/** * Initialize FreeMarkerConfigurationFactory's Configuration * if not overridden by a pre-configured 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(); } }
/** * 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; }
private Charset getCharset(@Nullable MediaType mediaType) { return Optional.ofNullable(mediaType).map(MimeType::getCharset).orElse(getDefaultCharset()); }
/** * Build a FreeMarker template model for the given model Map. * <p>The default implementation builds a {@link SimpleHash}. * @param model the model to use for rendering * @param exchange current exchange * @return the FreeMarker template model, as a {@link SimpleHash} or subclass thereof */ protected SimpleHash getTemplateModel(Map<String, Object> model, ServerWebExchange exchange) { SimpleHash fmModel = new SimpleHash(getObjectWrapper()); fmModel.putAll(model); return fmModel; }
public FreeMarkerRegistration() { super(new FreeMarkerViewResolver()); getViewResolver().setSuffix(".ftl"); } }
@Test public void noTemplateName() throws Exception { this.exception.expect(IllegalArgumentException.class); this.exception.expectMessage("Property 'url' is required"); FreeMarkerView freeMarkerView = new FreeMarkerView(); freeMarkerView.afterPropertiesSet(); }
/** * Autodetect a {@link FreeMarkerConfig} object via the ApplicationContext. * @return the Configuration instance to use for FreeMarkerViews * @throws BeansException if no Configuration instance could be found * @see #setConfiguration */ protected FreeMarkerConfig autodetectConfiguration() throws BeansException { try { return BeanFactoryUtils.beanOfTypeIncludingAncestors( obtainApplicationContext(), FreeMarkerConfig.class, true, false); } catch (NoSuchBeanDefinitionException ex) { throw new ApplicationContextException( "Must define a single FreeMarkerConfig bean in this web application context " + "(may be inherited): FreeMarkerConfigurer is the usual implementation. " + "This bean may be given any name.", ex); } }
/** * 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(); Version version = Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS; return (ow != null ? ow : new DefaultObjectWrapperBuilder(version).build()); }