如何在JSP中配置FreemarkerTag

时间:2013-09-13 13:57:41

标签: freemarker taglib

我尝试在JSP中使用FreemarkerTag。我有点疑惑,因为发行版缺少TLD文件,但我自己创建了一个:

<?xml version="1.0" encoding="UTF-8"?>
<taglib>
    <tlibversion>1.0</tlibversion>
    <jspversion>1.1</jspversion>
    <shortname>fmtag</shortname>
    <info>Taglib for freemarker in JSP</info>
    <uri>http://adesso.ch/jsp/fmtag</uri>
  <tag>
    <name>template</name>
    <tagclass>freemarker.ext.jsp.FreemarkerTag</tagclass>
    <bodycontent>tagdependent</bodycontent>
    <info>Allows evaluation of FreeMarker templates inside JSP</info>
    <attribute>
      <name>caching</name>
      <required>false</required>
    </attribute>
  </tag>
</taglib>

我可以使用FreemarkerTag并执行基本操作(如字符串连接+循环)。我不能使用它作为例如使用这样的include指令:

<fm:template>
  <#include "dummy_product_box_page_67623.ftl">
</fm:template>

在这种情况下,将找不到要包含的模板。我不知道如何更详细地配置我的主模板。特别是,对于include指令,我找不到配置TemplatePath的方法。 我在配置对象上通过setDirectoryForTemplateLoading()以编程方式找到了很多示例,但我的方案中没有编程访问。

任何提示和想法都将受到高度赞赏。感谢。

1 个答案:

答案 0 :(得分:1)

它使用Configuration.getDefaultConfiguration(),因此这是您必须在某个早期阶段配置的配置实例,例如最好在启动任何HTTP请求处理线程之前。但请注意,Configuration.getDefaultConfiguration()已被弃用,因为如果某个其他组件(您甚至不知道的组件)也会为自己设置单例。但是现在这就是那个糟糕的废弃标签的情况......(我想它应该从Servlet请求范围中获取Configuration作为属性,并且从应用程序上下文中失败。)。