It is actually an easy task if you know how to do it. All you need is already embedded into FreeMarker, for instance it is TaglibFactory.ClasspathMetaInfTldSource
class. I spend several hours to investigate that problem, so I want to share a solution.
I implemented it as BeanPostProcessor
because now there is no way to set TaglibFactory
before FreeMarkerConfigurer
bean is initialized.
import freemarker.ext.jsp.TaglibFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import java.util.Arrays;
import java.util.regex.Pattern;
/**
* A {@link BeanPostProcessor} that enhances {@link FreeMarkerConfigurer} bean, adding
* {@link freemarker.ext.jsp.TaglibFactory.ClasspathMetaInfTldSource} to {@code metaInfTldSources}
* of {@link TaglibFactory}, containing in corresponding {@link FreeMarkerConfigurer} bean.
*
* <p>
* This allows JSP Taglibs ({@code *.tld} files) to be found in classpath ({@code /META-INF/*.tld}) in opposition
* to default FreeMarker behaviour, where it searches them only in ServletContext, which doesn't work
* when we run in embedded servlet container like {@code tomcat-embed}.
*
* @author Ruslan Stelmachenko
* @since 20.02.2019
*/
@Component
public class JspTagLibsFreeMarkerConfigurerBeanPostProcessor implements BeanPostProcessor {
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof FreeMarkerConfigurer) {
FreeMarkerConfigurer freeMarkerConfigurer = (FreeMarkerConfigurer) bean;
TaglibFactory taglibFactory = freeMarkerConfigurer.getTaglibFactory();
TaglibFactory.ClasspathMetaInfTldSource classpathMetaInfTldSource =
new TaglibFactory.ClasspathMetaInfTldSource(Pattern.compile(".*"));
taglibFactory.setMetaInfTldSources(Arrays.asList(classpathMetaInfTldSource));
// taglibFactory.setClasspathTlds(Arrays.asList("/META-INF/tld/common.tld"));
}
return bean;
}
}
The only restriction is that *.tld
files must have <uri>
xml tag inside. All standard spring/spring-security TLDs have it. And also these files must be inside META-INF
folder of classpath, like META-INF/mytaglib.tld
. All standard spring/spring-security TLDs are also follow this convention.
Commented line is just for example of how you can add "custom" paths of *.tld
files if for some reason you can't place them into standard location (maybe some external jar, which doesn't follow the convention). It can be extended to some sort of classpath scanning, searching for all *.tld
files and adding them into classpathTlds
. But usually it is just doesn't required if your TLDs follow JSP conventions to be placed inside META-INF
directory.
I have tested this in my FreeMarker template and it works:
<#assign common = JspTaglibs["http://my-custom-tag-library/tags"]>
<#assign security = JspTaglibs["http://www.springframework.org/security/tags"]>
<#assign form = JspTaglibs["http://www.springframework.org/tags/form"]>
<#assign spring = JspTaglibs["http://www.springframework.org/tags"]>
For custom tag ("http://my-custom-tag-library/tags") to work, it must be *.tld
file in src/main/resources/META-INF/some.tld
and it must contain the <uri>
xml tag, like <uri>http://my-custom-tag-library/tags</uri>
. It will be found by FreeMarker then.
I hope it helps someone to save several hours to find "right" solution for this problem.
Tested with spring-boot v2.0.5.RELEASE