All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.peterphi.std.guice.thymeleaf.TemplateResolverProvider Maven / Gradle / Ivy

There is a newer version: 7.7.2
Show newest version
package com.peterphi.std.guice.thymeleaf;

import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.name.Named;
import com.peterphi.std.annotation.Doc;
import com.peterphi.std.threading.Timeout;
import org.thymeleaf.templateresolver.ITemplateResolver;
import org.thymeleaf.templateresolver.ServletContextTemplateResolver;

import java.util.concurrent.TimeUnit;

public class TemplateResolverProvider implements Provider
{
	@Inject(optional = true)
	@Named("thymeleaf.cache-ttl")
	@Doc("The maximum Time-To-Live value on the thymeleaf in-memory template cache")
	Timeout cacheTTL = new Timeout(1, TimeUnit.HOURS);


	@Override
	public ITemplateResolver get()
	{
		ServletContextTemplateResolver resolver = new ServletContextTemplateResolver();

		resolver.setTemplateMode("HTML5");

		// Load templates from WEB-INF/templates/{name}.html
		resolver.setPrefix("/WEB-INF/template/");
		resolver.setSuffix(".html");

		if (cacheTTL.getMilliseconds() > 0)
		{
			// cache templates for an hour
			resolver.setCacheTTLMs(cacheTTL.getMilliseconds());
			resolver.setCacheable(true);
		}
		else
		{
			// Don't cache
			resolver.setCacheable(false);
		}

		return resolver;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy