Freemarker Rendering

798 views
Skip to first unread message

Hans Liebenberg

unread,
Jan 21, 2016, 2:05:13 PM1/21/16
to ninja-framework
Hi,

public Result userDashboard(
        @PathParam("email") String email,
        @PathParam("id") Integer id,
        Context context) {

    Result result = Results.html();

    result.render("id", Integer.toString(id));
    result.render("email", email);

    return result;

}

I am battling to pass a POJO to my render() method. i.e in the example above it all works s expected, but if I have a POJO with name  and id members, and pass in
result.render("myPojo", thePojo);

Then from the freemarker  ftl I cannot acces myPojo.name or myPojo.id ...

I get a freemarker.core.InvalidReferenceException exception , but yet the name and id values are populated.

Thanks

metacity (Mikko Oksa)

unread,
Jan 22, 2016, 2:24:12 AM1/22/16
to ninja-framework
Hi,

This might be completely anecdotal, but do you have bean style getters (getName(), getId()) for the pojo fields? Freemarker could be expecting them..

Raphael André Bauer

unread,
Jan 22, 2016, 3:43:10 AM1/22/16
to ninja-f...@googlegroups.com
Without more code it is really hard to tell what is going wrong...
Cheers,

Raphael
> --
> You received this message because you are subscribed to the Google Groups
> "ninja-framework" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ninja-framewo...@googlegroups.com.
> To post to this group, send email to ninja-f...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ninja-framework/e0da95ea-116f-4a5c-aff8-b924a9787924%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Hans Liebenberg

unread,
Jan 22, 2016, 4:41:10 AM1/22/16
to ninja-f...@googlegroups.com
Hi

Thanks, Yes I do have getters

--
You received this message because you are subscribed to a topic in the Google Groups "ninja-framework" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ninja-framework/RZ8skgQyo04/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ninja-framewo...@googlegroups.com.

To post to this group, send email to ninja-f...@googlegroups.com.

Hans Liebenberg

unread,
Jan 22, 2016, 4:56:51 AM1/22/16
to ninja-f...@googlegroups.com
Hi Raphael,

Here is my Java code:
public class ApplicationController {

public Result index() throws Exception{


        Result result =  Results.html();

        result.render("tester", new Tester("xxxx", "yyy"));        
        result.render("simpleValue", "simple");

return result;


}

static class Tester {
String
name;
String
code;

public Tester(String name, String code) {
this.name = name;
this.code = code;
}

public String getName() {
return name;
}

public String getCode() {
return code;
}
}

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Then my FTL template
<p>
${tester.name}
</p>

>>>>> Error result
FreeMarker template error:
The following has evaluated to null or missing:
==> tester.name  [in template "views/ApplicationController/index.ftl.html" at line 9, column 46]

----
Tip: It's the step after the last dot that caused this error, not those before it.
----
Tip: If the failing expression is known to be legally refer to something that's sometimes null or missing, either specify a default value like myOptionalVar!myDefault, or use <#if myOptionalVar??>when-present<#else>when-missing</#if>. (These only cover the last step of the expression; to cover the whole expression, use parenthesis: (myOptionalVar.foo)!myDefault, (myOptionalVar.foo)??
----

----
FTL stack trace ("~" means nesting-related):
	- Failed at: ${tester.name} auto-escaped  [in template "views/ApplicationController/index.ftl.html" at line 9, column 44]
	~ Reached through: #nested  [in template "views/layout/homeLayout.ftl.html" in macro "myLayout" at line 49, column 9]
	~ Reached through: @layout.myLayout "Home page"  [in template "views/ApplicationController/index.ftl.html" at line 2, column 1]
----
>>>>>

However, if my template only specifies:
${simpleValue}
Then it works..

Thanks Alot!
Hans

Raphael André Bauer

unread,
Jan 22, 2016, 2:06:38 PM1/22/16
to ninja-f...@googlegroups.com
Hey Hans,


what happens when you either call ${tester.getName} OR make variable
name public and call ${tester.name}.


Cheers,

Raphael

On Fri, Jan 22, 2016 at 10:56 AM, Hans Liebenberg
> You received this message because you are subscribed to the Google Groups
> "ninja-framework" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ninja-framewo...@googlegroups.com.
> To post to this group, send email to ninja-f...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ninja-framework/CALi%3D%3DKaBbEVLrokkhPoT2k-M5Yq4XeWUBwUALBUQCpS9ZO3Z9w%40mail.gmail.com.

Hans Liebenberg

unread,
Jan 24, 2016, 1:36:12 AM1/24/16
to ninja-f...@googlegroups.com
Hi Raphael,

If I leave the getters, and make the name + code members public, and access in my template as tester.name, I get error:
FreeMarker template error:
An error has occurred when reading existing sub-variable "name"; see cause exception! The type of the containing value was: extended_hash+string (controllers.ApplicationController$Tester wrapped into f.e.b.StringModel)

and with a stack trace:

Caused by: java.lang.IllegalAccessException: Class freemarker.ext.beans.BeanModel can not access a member of class controllers.ApplicationController$Tester with modifiers "public"
	at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:109)

Then test 2:  Access the object in my template with tester.getName , with both cases of private vs public modifiers i get:

FreeMarker template error:
The following has evaluated to null or missing:
==> tester.getName  [in template "views/ApplicationController/index.ftl.html" at line 9, column 22]


using java version "1.7.0_71"

Will carry on playing

Thanks










Raphael André Bauer

unread,
Jan 24, 2016, 4:07:44 AM1/24/16
to ninja-f...@googlegroups.com
I guess if you make your Tester class public it should work...
Best,
Raphael

On Sun, Jan 24, 2016 at 7:36 AM, Hans Liebenberg
> https://groups.google.com/d/msgid/ninja-framework/CALi%3D%3DKbK%3Dqm-k%2BLdWpuj5CSWTR1T%3DbNO9LqPeJb2K1ZvykCsOg%40mail.gmail.com.

Hans Liebenberg

unread,
Jan 25, 2016, 1:05:50 AM1/25/16
to ninja-f...@googlegroups.com
Thanks Raphael !

I feel a little stupid!

Raphael André Bauer

unread,
Jan 25, 2016, 3:41:06 AM1/25/16
to ninja-f...@googlegroups.com
No... that just makes clear that the error messages generated by
Freemarker are not easy to get... No your fault..

Cheers,

Raphael

On Mon, Jan 25, 2016 at 7:05 AM, Hans Liebenberg
> https://groups.google.com/d/msgid/ninja-framework/CALi%3D%3DKY3ag%3DsSkopWsV0tcVHqv4Z7fQKOQVEcNt39j3%3D5BkabA%40mail.gmail.com.

Joe Lauer

unread,
Jan 25, 2016, 11:22:23 AM1/25/16
to ninja-framework
Hans,

You may be interested in checking out Rocker templates -- the compiler would have caught your simple & common error.


-Joe

Reply all
Reply to author
Forward
0 new messages