FreeMarker template error: The following has evaluated to null or missing | BUT NOT TRUE

柔情痞子 提交于 2019-12-09 17:37:12

问题


The error I'm facing is so weird. Everything looks fine, but I get this error when the browser sends the GET request to the server. What I'm trying to do is actually catching the HTTP parameters, save them in an object saved in an ArrayList sending to a Freemarker template.

Could you please help me? Thanks a lot.

The error:

freemarker.log._JULLoggerFactory$JULLogger error SEVERE: Error executing FreeMarker template FreeMarker template error: The following has evaluated to null or missing: ==> item.lat1 [in template "view/result.ftl" at line 18, column 15]

freemarker.core.InvalidReferenceException: [... Exception message was already printed; see it above ...] at freemarker.core.InvalidReferenceException.getInstance(InvalidReferenceException.java:131) at freemarker.core.EvalUtil.coerceModelToString(EvalUtil.java:355) at freemarker.core.Expression.evalAndCoerceToString(Expression.java:82) at freemarker.core.DollarVariable.accept(DollarVariable.java:41) at freemarker.core.Environment.visit(Environment.java:324) at freemarker.core.MixedContent.accept(MixedContent.java:54) at freemarker.core.Environment.visitByHiddingParent(Environment.java:345) at freemarker.core.IteratorBlock$IterationContext.executeNestedBlockInner(IteratorBlock.java:268) at freemarker.core.IteratorBlock$IterationContext.executeNestedBlock(IteratorBlock.java:220) at freemarker.core.IteratorBlock$IterationContext.accept(IteratorBlock.java:194) at freemarker.core.Environment.visitIteratorBlock(Environment.java:572) at freemarker.core.IteratorBlock.acceptWithResult(IteratorBlock.java:78) at freemarker.core.IteratorBlock.accept(IteratorBlock.java:64) at freemarker.core.Environment.visit(Environment.java:324) at freemarker.core.MixedContent.accept(MixedContent.java:54) at freemarker.core.Environment.visit(Environment.java:324) at freemarker.core.Environment.process(Environment.java:302) at freemarker.template.Template.process(Template.java:325) at spark.template.freemarker.FreeMarkerEngine.render(FreeMarkerEngine.java:71) at controller.App.lambda$main$1(App.java:57) at spark.RouteImpl$1.handle(RouteImpl.java:58) at spark.webserver.MatcherFilter.doFilter(MatcherFilter.java:162) at spark.webserver.JettyHandler.doHandle(JettyHandler.java:61) at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:189) at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141) at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:119) at org.eclipse.jetty.server.Server.handle(Server.java:517) at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:302) at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:242) at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:245) at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:95) at org.eclipse.jetty.io.SelectChannelEndPoint$2.run(SelectChannelEndPoint.java:75) at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.produceAndRun(ExecuteProduceConsume.java:213) at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.run(ExecuteProduceConsume.java:147) at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:654) at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:572) at java.lang.Thread.run(Thread.java:745)

[qtp285763673-17] ERROR spark.webserver.MatcherFilter - java.lang.IllegalArgumentException: freemarker.core.InvalidReferenceException: The following has evaluated to null or missing: ==> item.lat1 [in template "view/result.ftl" at line 18, column 15]

.. But true, because I do print the Array and it works fine!

  final FreeMarkerEngine freeMarkerEngine = new FreeMarkerEngine();
    final Configuration freeMarkerConfiguration = new Configuration();
    freeMarkerConfiguration.setTemplateLoader(new ClassTemplateLoader(App.class, "/"));
    freeMarkerEngine.setConfiguration(freeMarkerConfiguration);

    get("/rest", (request, response) -> {
        Double lat1 = Double.parseDouble(request.queryParams("lat1") != null ? request.queryParams("lat1") : "anonymous");
        Double lon1 = Double.parseDouble(request.queryParams("lon1") != null ? request.queryParams("lon1") : "anonymous");
        Double lat2 = Double.parseDouble(request.queryParams("lat2") != null ? request.queryParams("lat2") : "anonymous");
        Double lon2 = Double.parseDouble(request.queryParams("lon2") != null ? request.queryParams("lon2") : "anonymous");

        if (shouldReturnHtml(request)) {
            response.status(200);
            response.type("text/html");
            Map<String, Object> attributes = new HashMap<>();
            attributes.put("list",loadTheList(lat1,lon1,lat2,lon2));
            return freeMarkerEngine.render(new ModelAndView(attributes, "/view/result.ftl"));
        }
        else {
            response.status(200);
            response.type("application/json");
            return null;
        }
    });


}

private static boolean shouldReturnHtml(Request request) {
    String accept = request.headers("Accept");
    return accept != null && accept.contains("text/html");
}
public static ArrayList<Bereken> loadTheList(double lat1, double lon1, double lat2, double lon2) {
    ArrayList<Bereken> list = new ArrayList<>();
    list.add(new Bereken(lat1,lon1,lat2,lon2));
    return list;
}

And result.ftl:

      <#list list as item>
        <h2>${item.lat1}</h2>
        <h2>${item.lon1}</h2>
        <h2>${item.lat2}</h2>
        <h2>${item.lon2}</h2>
    </#list>

回答1:


You are adding 1 instance of the Bereken class to the "list". So i would assume the Bereken class does not provide public properties lat1, lat2, lon1, lon2 or coresponding getter methods.




回答2:


I just ran across this yesterday. I was working through a tutorial and one of the steps did not include the code for a getter method in a class that created an object. I guess the author just assumed the reader knew that should have happened already.

So, wherever this 'Bereken' thing is (I just learned it is a JavaBean class), be sure to add the setter and getter methods for the properties associated with each element in the 'list'.




回答3:


I know this is an old question, but I just bumped into this. Freemarker will also complain that the value "is null or missing" if you pass it an instance of a private class (which I just tried). If you pass in a Java Bean class, it needs to be public. The error message is very misleading in this case.



来源:https://stackoverflow.com/questions/35805983/freemarker-template-error-the-following-has-evaluated-to-null-or-missing-but

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!