0

I want to get bean values in my freemaker tempalate. For that what i am doing is ?

In my java class :

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

I also created the setter and getter.

In tempalate when i am doing this :

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

It is printing my class package name but when i am doing this in tempalate :

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

Name is the property that i have defined in my bean and value is ranveer but its not printing this value. The StackTrace is :

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)
  • 1
    Maybe you should add the code of your bean here. Because what you do in the example above is creating an empty bean and put it into the session. Freemarker does not like empty values, so it bails out. – thst Oct 31 '13 at 15:09
  • no i have set a value into the bean also. Its not an empty bean. Also created setter and getter. I have edited my code. Please have a look . – Ranveer Singh Rajpurohit Nov 02 '13 at 04:25

2 Answers2

1

if your MyBean does not a have a getName()-method then ${bean.name} is undefined

alfrescian
  • 4,079
  • 1
  • 18
  • 20
  • there is setter and getter for name property. I have edited my code.please have a look. Still error is undifined. – Ranveer Singh Rajpurohit Nov 02 '13 at 04:26
  • You need to expose the session attributes explicitely. This is not standard in the freemarker view resolver in spring. See my answer below. – thst Nov 08 '13 at 16:52
0

please add the following property to your freemarker view resolver in your spring beans.xml

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

and try again.

thst
  • 4,592
  • 1
  • 26
  • 40