0

我想在我的 freemaker 模板中获取 bean 值。为此,我正在做的是什么?

在我的 java 类中:

MyBean myBean= new MyBean();
myBean.setName("ranveer");
session.setAttribute("MyBean", myBean);

我还创建了 setter 和 getter。

当我这样做时,在模板中:

<#assign bean= Session.MyBean>
    ${bean}

它正在打印我的类包名称,但是当我在模板中执行此操作时:

 <#assign bean= Session.MyBean>
        ${bean.name}

名称是我在我的 bean 中定义的属性,值是 ranveer 但它不打印这个值。StackTrace 是:

The Web Script /Test/service/inbox/sidebar has responded with a status of 500 - Internal Error.

500 Description:    An error inside the HTTP server which prevented it from fulfilling the request.

Message:    09310010 Failed to process template webscripts/inbox/inbox.sidebar.get.html.ftl

Exception:  freemarker.core.InvalidReferenceException - Expression ${bean.name} is undefined on line 5, column 11 in webscripts/inbox/inbox.sidebar.get.html.ftl.

    freemarker.core.TemplateObject.assertNonNull(TemplateObject.java:124)
    freemarker.core.Expression.getStringValue(Expression.java:118)
    freemarker.core.Expression.getStringValue(Expression.java:93)
    freemarker.core.DollarVariable.accept(DollarVariable.java:76)
    freemarker.core.Environment.visit(Environment.java:208)
    freemarker.core.MixedContent.accept(MixedContent.java:92)
    freemarker.core.Environment.visit(Environment.java:208)
    freemarker.core.Environment.process(Environment.java:188)
    freemarker.template.Template.process(Template.java:237)
    org.springframework.extensions.webscripts.processor.FTLTemplateProcessor.process(FTLTemplateProcessor.java:156)
    org.springframework.extensions.webscripts.AbstractWebScript.renderTemplate(AbstractWebScript.java:580)
    org.springframework.extensions.webscripts.DeclarativeWebScript.renderFormatTemplate(DeclarativeWebScript.java:263)
    org.springframework.extensions.webscripts.DeclarativeWebScript.execute(DeclarativeWebScript.java:143)
    org.springframework.extensions.webscripts.PresentationContainer.executeScript(PresentationContainer.java:69)
    org.springframework.extensions.webscripts.LocalWebScriptRuntimeContainer.executeScript(LocalWebScriptRuntimeContainer.java:203)
    org.springframework.extensions.webscripts.AbstractRuntime.executeScript(AbstractRuntime.java:333)
    org.springframework.extensions.webscripts.AbstractRuntime.executeScript(AbstractRuntime.java:189)
4

2 回答 2

1

如果您的 MyBean 没有 getName() 方法,则 ${bean.name} 未定义

于 2013-11-01T13:43:07.067 回答
0

请在您的 spring beans.xml 中将以下属性添加到您的 freemarker 视图解析器

<property name="exposeSessionAttributes">
    <value>true</value>
</property>

然后再试一次。

于 2013-11-07T14:41:59.913 回答