Search code examples
jspstruts2freemarker

Getting "Freemarker template Error" while using <@s.generator and iterator tags


I am learning Struts 2. While creating views using FreeMarker template, I am getting an error. I am using Eclipse IDE and running the application in Tomcat 8.

Freemarker Code:

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
        <h1>Sample...(FreeMarker)</h1><br><br>      
        <div>
            <@s.generator separator="," val="%{'15,20,30,40,50'}">
                Last entered protein Values -:<br>
                <@s.iterator>
                    <@s.property /><br>
                </@s.iterator> 
            </@s.generator> 
        </div>      
    </body>
    </html>

Eclipse Console Trace Log:

    > Jul 14, 2016 8:19:02 PM
    > com.opensymphony.xwork2.util.logging.jdk.JdkLogger error SEVERE:
    > Exception occurred during processing request: The following has
    > evaluated to null or missing:
    > ==> s.generator  [in template "WEB-INF/content/enter-protein.ftl" at line 10, column 11]
    > 
    > ---- 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 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: @s.generator separator="," val="%{'15...  [in template "WEB-INF/content/enter-protein.ftl" at line 16, column 9]
    > ---- FreeMarker template error: The following has evaluated to null or missing:
    > ==> s.generator  [in template "WEB-INF/content/enter-protein.ftl" at line 16, column 11]
    > 
    > ---- 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 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: @s.generator separator="," val="%{'15...  [in template "WEB-INF/content/enter-protein.ftl" at line 16, column 9]
    > ----
    > 
    > Java stack trace (for programmers):
    > ---- freemarker.core.InvalidReferenceException: [... Exception message was already printed; see it above ...]     at
    > freemarker.core.InvalidReferenceException.getInstance(InvalidReferenceException.java:116)
    >   at freemarker.core.UnifiedCall.accept(UnifiedCall.java:112)     at
    > freemarker.core.Environment.visit(Environment.java:312)   at
    > freemarker.core.MixedContent.accept(MixedContent.java:62)     at
    > freemarker.core.Environment.visit(Environment.java:312)   at
    > freemarker.core.Environment.process(Environment.java:290)     at
    > freemarker.template.Template.process(Template.java:312)   at
    > org.apache.struts2.views.freemarker.FreemarkerResult.doExecute(FreemarkerResult.java:223)
    >   at
    > org.apache.struts2.dispatcher.StrutsResultSupport.execute(StrutsResultSupport.java:191)   at
.
.
.
.
    ........................ So on...........

Weird thing is, when I try the same in JSP, it works flawlessly! (In JSP, we use <s:something> instead of <@s.something> )


Solution

  • The <s:generator> tag is simple not that kind of a tag* that can be used in FreeMarker template.

    The good part is: It isn't actually needed at all. You can use <s:iterator> tag w/o <s:generator>.

    Use value attribute of iterator tag to set iteratable source and OGNL expression {e1,e2,e3} to create a list.

    <@s.iterator value="{15,20,30,40,50}">
        <@s.property /><br>
    </@s.iterator> 
    

    *Wondering which Struts2 tags you can use in FreeMarker template? See DefaultTagLibrary#getDirectiveClasses method.