1

这是我的场景:</p>

1、在行动中,变量id是declear就像吹(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)

我的代码有问题吗?

4

1 回答 1

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

由于您没有发布整个操作类,因此很难说出您将哪些模型数据放入您的操作中。

于 2011-03-31T13:24:24.890 回答