cache FreeMarker templates

I'm using the Spring class FreeMarkerConfigurationFactoryBean to retrieve FreeMarker templates. I would like these templates to be cached, but there doesn't appear to be any way to indicate that this behaviour is required.

In contrast, Spring modules provides a CachingTemplateResolver which does provide template caching, but is it possible to achieve this without using Spring modules, i.e. with FreeMarkerConfigurationFactoryBean?

Cheers, Don

Asked by: Julia802 | Posted: 21-01-2022

Answer 1

From the Freemarker manual, it seems caching is on by default.

Template caching

FreeMarker caches templates (assuming you use the Configuration [which the Spring MBean does...] methods to create Template objects). This means that when you call getTemplate, FreeMarker not only returns the resulting Template object, but stores it in a cache, so when next time you call getTemplate with the same (or equivalent) path, it just returns the cached Template instance, and will not load and parse the template file again.

Answered by: Ted243 | Posted: 22-02-2022

Similar questions

java - Load FreeMarker templates from database

I would like to store my FreeMarker templates in a database table that looks something like: template_name | template_content --------------------------------- hello |Hello ${user} goodbye |So long ${user} When a request is received for a template with a particular name, this should cause a query to be executed, which loads the relevant template content. This template content...

java - Using JAX-RS / Jersey with Freemarker templates

There are examples on the web showing how to use the JAX-RS implementation Jersey with custom template engines like FreeMarker. But these examples are looking a bit improvised or

java - In Freemarker templates, how do you get the name of the data model's class?

I'm using Freemarker to generate an XHTML page from a POJO which contains a List of Objects, all of which are instances of subclasses of the same parent class. I'd like to generate different information for each object based on its class. How do you test for that in the Freemarker language? For example, if I had a List, how would I determine if a particular Shape was a Rectangle or a Circle?

java - freemarker templates in several jars

How can I configure freemarker to search templates in several jars? With spring. <!-- freemarker config --> <bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"> <property name="templateLoaderPaths" value="classpath:/freemarker/" /> </bean> One war file (to deploy), and jar file (in dependencies). O...

java - Freemarker Looking For Default Templates

I set up a template directory through struts.ui.templateDir in my struts.xml. Now, when I access my page, a comes up looking for a bunch of files that I can only guess Freemarker is looking for by default. Here is one such file: Template /WEB-INF/templates/simple/form-close.ftl not found. I do not want this file or any ot...

MongoDB, Java and Freemarker Templates (FLT)

I'm working on this web project with: Java, MongoDB and Freemarker Templates (FTL) The data is sent to the FTL pages within a Map. Now I would like to know if it's possible to convert the result from a Mongo query straight into a Map and then send this to the FTL template. For example: I would like to query all the members from a collection and put this in Map to use in the FTL template.

java - Freemarker Templates Caching Shared Variable after refresh

I have a Spring MVC web app which uses FreeMarker for rendering the views and have come up with the following issue. Within my FreeMarker config I declare a Singleton Spring bean as a FreeMarker variable and within my application I have provided the mechanism for the Singleton Bean to be dynamically refreshed (the bean contains the application config retrieved from the DB). Now the problem is that when it i...

java - Loading freemarker templates folder inside .jar

I am using freemarker templates in my application Before deploying my application to a jar file, all I needed to do in order to load my templates was this: cfg.setDirectoryForTemplateLoading(new File("templates")); Which loaded all templates from the template folder I created inside my project. Now, after moving to maven and deploying my application to an executable jar. The application...

java - Freemarker : nested templates

I am a beginner with freemarker and I would like to use it to generate some repetitive code. From a simple class like this one: public class Point { private Integer x; private Integer y; private String name; } I need, for each attribute, to generate lines like this: ValueProvider<Point,Integer> x(); ValueProvider<Point,Integer> y(); ValueProvider&l...

java - How to render freemarker templates that have a symlink in their path?

I am trying to use FreeMarker to render some templates that come from a CMS path that happens to include a symbolic link (under Linux). Our CMS code handles the path to the template so, for example, this path: /var/cms/live/display/main.html really points to: /var/cms/trunk/127/display/main.html /var/cms/live is the base-directory while

java - How to check if a variable exists in a FreeMarker template?

I have a Freemarker template which contains a bunch of placeholders for which values are supplied when the template is processed. I want to conditionally include part of the template if the userName variable is supplied, something like: [#if_exists userName] Hi ${userName}, How are you? [/#if_exists] However, the FreeMarker manual seems to indicate that if_exists is deprecated, but I can...

java - Load FreeMarker templates from database

I would like to store my FreeMarker templates in a database table that looks something like: template_name | template_content --------------------------------- hello |Hello ${user} goodbye |So long ${user} When a request is received for a template with a particular name, this should cause a query to be executed, which loads the relevant template content. This template content...

java - Building Freemarker from source

I am working on a security project at my university for open source software. I am trying to build freemarker from source, but I am running into some problems. Everything that I can find online says that simple running 'ant' in the root directory should build the project. The only requirement is that you have ant v1.7.0 installed, which I do. When I go into the root directory and type 'ant' I get the following out...

java - Implement a Custom Escaper in Freemarker

Freemarker has the ability to do text escaping using something like this: <#escape x as x?html> Foo: ${someVal} Bar: ${someOtherVal} </#escape> xml, xhtml, and html are all built in escapers. Is there a way to register a custom written escaper? I want to generate CSV and have each individual element escaped and that seems like a good mechanism. I'm trying to do this in ...

java - Using An Absolute Path With FreeMarker

I've been using FreeMarker for a little while now, but there's one glaring piece of functionality that is either missing or I just can't figure out (I hope the latter!). If you pass cfg.getTemplate() an absolute path, it just doesn't work. I know you can specify a template directory, but I can't afford to do that, my use case could deal with files...

java - Default escaping in Freemarker

In Freemarker templates we can use the escape directive to automatically apply an escaping to all interpolations inside the included block: <#escape x as x?html> <#-- name is escaped as html --> Hallo, ${name} </#escape> Is there a way to programmatically achieve a similar effect, defining a default escape applied to all interpolations in the template, including those...

How do I call java methods on an object from a FreeMarker template?

Is it possible to call a method that takes parameters from a Freemarker template? I have an object model that I'm trying to render with Freemarker into a web page. One of the objects has a method to get a sublist of it's contents - taking a parameter that is used to filter the list: public List getunits(final String type); I know in JSP you can't do this directly, but you can write cust...

java - Velocity vs. FreeMarker

Closed. This question does not meet Stack Overflow guid...

java - FreeMarker Editor in eclipse 3.5 giving Illegal Argument Exception

I am trying to use the freemarker editor in eclipse 3.5 Galileo and found the exception : java.lang.IllegalArgumentException: Argument not valid at org.eclipse.swt.SWT.error( at org.eclipse.swt.SWT.error( at org.eclipse.swt.SWT.error( at org.eclipse.swt.custom.StyledText.setStyleRanges( at

java - Indirect reference in Freemarker

Using freemarker, is there any way to reference a value indirectly along the lines of... <#assign apple=fruit> <#assign item=apple> ${${item}} to produce the output 'fruit' without prior knowledge of what value item might contain?

Still can't find your answer? Check out these amazing Java communities for help...

Java Reddit Community | Java Help Reddit Community | Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)