关于在Freemarker文件中使用Struts2包含标记的问题

时间:2011-03-25 05:58:39

标签: macros struts2 jsp-tags freemarker oscache

这是我的场景:

1,在操作中,变量id像打击一样清除(TestAction.java):

… 
private Integer id;
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
} 
…

2,web.xml中的配置:

…
<filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
        <dispatcher>REQUEST</dispatcher>
        <dispatcher>FORWARD</dispatcher>
        <dispatcher>INCLUDE</dispatcher>
    </filter-mapping>
…
<servlet>
        <servlet-name>JSPSupportServlet</servlet-name>
        <servlet-class>org.apache.struts2.views.JspSupportServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
</servlet>
…

3,在freemarker文件(test.ftl)中调用:

…
  <@s.include value="/invitation/inviter"/><#-- there is no variable id in this action -->
…
  <span>${id}<span>
…

4,错误消息:

Error on line 63, column 61 in WEB-INF/ftl/invitation/ test.ftl
Expecting a string, date or number here, Expression id!0 is instead a freemarker.ext.beans.ArrayModel
The problematic instruction:
----------
==> ${id} [on line 63, column 59 in WEB-INF/ftl/invitation/test.ftl]
in user-directive page [on line 1, column 1 in WEB-INF/ftl/invitation/ test.ftl]
----------

Java backtrace for programmers:
----------
freemarker.core.NonStringException: Error on line 63, column 61 in WEB-INF/ftl/invitation/ test.ftl
Expecting a string, date or number here, Expression id!0 is instead a freemarker.ext.beans.ArrayModel
         at freemarker.core.Expression.getStringValue(Expression.java:126)
         at freemarker.core.Expression.getStringValue(Expression.java:93)
         at freemarker.core.DollarVariable.accept(DollarVariable.java:76)
         at freemarker.core.Environment.visit(Environment.java:210)

我的代码有问题吗?

1 个答案:

答案 0 :(得分:0)

据我所知,你试图显示TestAction对象而不是ID。

尝试这样做:


${testAction.id}

或在您目前的情况下:

${id.id}

这相当于:

(new TestAction()).getId()

existingTestAction.getId()

Taka在这里看到这个答案:Getting template text from FreeMarker in Struts2 app

此链接:http://struts.apache.org/2.0.6/docs/using-freemarker-templates.html

由于您未发布整个操作类,因此很难说出您在Action中添加了哪些模型数据。