freemarker templates in several jars

本小妞迷上赌 提交于 2019-11-30 19:11:56

I found solution! Turn off preferFileSystemAccess to always load via SpringTemplateLoader.

    <!-- freemarker config -->
<bean id="freemarkerConfig"
    class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
    <property name="templateLoaderPaths" value="classpath:/freemarker/" />
    <property name="preferFileSystemAccess" value="false" />
</bean>

public void setPreferFileSystemAccess(boolean preferFileSystemAccess)

Set whether to prefer file system access for template loading. File system access enables hot detection of template changes.

If this is enabled, FreeMarkerConfigurationFactory will try to resolve the specified "templateLoaderPath" as file system resource (which will work for expanded class path resources and ServletContext resources too).

Default is "true". Turn this off to always load via SpringTemplateLoader (i.e. as stream, without hot detection of template changes), which might be necessary if some of your templates reside in an expanded classes directory while others reside in jar files.

Once I did a similar thing programmatically :

public class ControllerServlet extends HttpServlet {
  private Configuration cfg; 
  public void init() {
    cfg = new Configuration();
    // 1
    WebappTemplateLoader wtl = new WebappTemplateLoader(getServletContext(), "WEB-INF/templates");
    // 2
    ClassTemplateLoader ctl = new ClassTemplateLoader(ControllerServlet.class, "templates");
    MultiTemplateLoader mtl = new MultiTemplateLoader(new TemplateLoader[] {wtl, ctl});
    cfg.setTemplateLoader(mtl);
    //....
 }
}

ControllerServlet is a base class for my real servlets.

Number 1 sets the search path for templates to {{WEB-INF/templates}} in the current servlet context. Number 2 sets a second search path to the subpackage "templates".

You may add more template paths.

Hope it helps.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!