如何从会话中打印bean值?

时间:2013-10-31 14:48:59

标签: java spring alfresco freemarker alfresco-share

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

在我的java类中:

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

我还创建了setter和getter。

当我这样做时,我会发出诅咒:

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

它正在打印我的类包名,但是当我在tempalate中这样做时:

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

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)

2 个答案:

答案 0 :(得分:1)

如果你的MyBean没有getName()方法,那么$ {bean.name}是未定义的

答案 1 :(得分:0)

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

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

再试一次。