我已经使用 freemarker 作为视图模板编写了一个 Jsp 标签库。
custom.tld
放在 META-INF/tags/ 中。
我的标签 Lib 捆绑为 Jar。customTag.jar
. 它有以下模板
AlertTag.ftl
ButtonTag.ftl
AlertTag.ftl
取决于ButtonTag.ftl
.
AlertTag.ftl
:-
<#assign custom = JspTaglibs["http://www.abc.com/taglibs/custom"]>
<h5>${this.title}</h5>
<div class="alert ${this.typeClass} ${this.classes!}">
<@custom.button btnType="close" <span class="alt">close</span></@custom.button>
</div>
此 customTag.jar 是另一个 Web 应用程序的一部分。
web.xml
:-
<taglib>
<taglib-uri>http://www.abc.com/taglibs/custom</taglib-uri>
<taglib-location>/WEB-INF/custom.tld</taglib-location>
</taglib>
现在,当index.ftl
在网络应用程序中尝试使用标签<@custom.Alert id="abc">
时,它会引发以下异常
Expression JspTaglibs is undefined on line 1, column 17 in freemarker/AlertTag.ftl.
The problematic instruction:
----------
==> assignment: custom=JspTaglibs["http://www.abc.com/taglibs/custom"] [on line 1, column 1 in freemarker/AlertTag.ftl]
----------
Java backtrace for programmers:
----------
freemarker.core.InvalidReferenceException: Expression JspTaglibs is undefined on line 1, column 17 in freemarker/AlertTag.ftl.
我无法在 freemarker 中使用嵌套的 jsp 标签。
AlertTag.java
Configuration cfg = new Configuration();
cfg.setClassForTemplateLoading(this.getClass(), "/");
Template ftlTemplate = cfg.getTemplate(view);
JspWriter writer = pageContext.getOut();
data.put("id", id)
Map<String, Object> rootMap = new HashMap<String,Object>();
rootMap.put("this", data);
Environment environment = ftlTemplate.createProcessingEnvironment(rootMap, writer);
environment.process();
//ftlTemplate.process(rootMap, writer);
@Daniel,我已经使用了上面的环境,但它也没有工作。我是否正确使用它?