【问题标题】:Struts2 + Freemarker + DisplayTag: how to make it workStruts2 + Freemarker + DisplayTag:如何使其工作
【发布时间】:2011-03-15 19:12:15
【问题描述】:

我目前正在将我们的应用程序从 Webwork 升级到 Struts2。今天遇到一个奇怪的错误:displayTag 升级后停止工作。

这是从我的 FTL 文件中截取的:

@display.table>

我得到的错误是:

freemarker.template.TemplateModelException:
javax.servlet.jsp.JspException:异常:
[.LookupUtil] 查找属性“folderName”时出错
在对象类型“freemarker.template.SimpleSequence”中。
原因:未知属性“文件夹名称”

标准的 struts 标签工作正常,我在我的配置中添加了JspSupportServlet。知道为什么这不起作用吗?

【问题讨论】:

    标签: struts2 freemarker displaytag


    【解决方案1】:

    我找到了解决这个问题的方法(不确定它是唯一的方法还是最好的方法,但对我有用)。

    问题的根源在于freemarker.template.SimpleSequence 没有开箱即用地实现任何标准的 Collections API,它不是 Collection、Enumerable 等。

    为了解决这个问题,我创建了自定义FreemarkerManager 并提供了自定义BeansWrapper

    @覆盖 受保护的 BeansWrapper getObjectWrapper() { BeansWrapper 包装器 = super.getObjectWrapper(); 类 CustomBeansWrapper 扩展 BeansWrapper { 私有 BeansWrapper internalWrapper; 公共 Xp2BeansWrapper(BeansWrapper 包装器){ this.internalWrapper = 包装器; } //委托方法 公共模板模型包装(对象对象)抛出 TemplateModelException { TemplateModel 模型 = internalWrapper.wrap(object); 如果(模型实例SimpleSequence){ 类 SimpleSequenceWithIterator 扩展 SimpleSequence { 私有 SimpleSequence 内部序列; 公共 SimpleSequenceWithIterator(SimpleSequence 序列) { this.internalSequence = 序列; } //委托方法 //内部使用来自 Apache Commons 的 IteratorUtils //通过DisplayTag库,可以使用public iterator()方法 公共迭代器 iterator() 抛出 TemplateModelException { 返回到列表()。迭代器(); } } 返回新的 SimpleSequenceWithIterator((SimpleSequence) 模型); } 返回模型; } } return new CustomBeansWrapper(wrapper); }

    现在我只需要更改struts.properties 中的一项设置:

    struts.freemarker.manager.classname=xyz.CustomFreemarkerManager
    

    【讨论】:

    • 设置参数object_wrapper的值可以设置成freemarker.ext.beans.BeansWrapper,保证不使用简单的wrapper
    【解决方案2】:

    您还可以验证您的部署是否将所有依赖项复制到 WEB-INF/lib。 TaglibFactory 在 jar 里面的 /META-INF/ 下搜索每个 TLD。

    看看https://stackoverflow.com/a/37092269/1113510

    【讨论】:

      猜你喜欢
      • 2011-01-26
      • 1970-01-01
      • 2011-10-10
      • 2011-12-03
      • 2012-06-25
      • 1970-01-01
      • 2010-10-21
      • 2018-05-16
      • 2017-07-19
      相关资源
      最近更新 更多