我想在我的 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)