java – FreeMarker模板错误:以下内容已评估为null或缺失但不是

我面临的错误是如此奇怪.一切看起来都很好,但是当浏览器将GET请求发送到服务器时,我收到此错误.我想要做的是实际捕获HTTP参数,将它们保存在发送到Freemarker模板的ArrayList中保存的对象中.

请你帮助我好吗?非常感谢.

错误:

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:[…异常消息是
  已打印;见上面……]
  freemarker.core.InvalidReferenceException.getInstance(InvalidReferenceException.java:131)
    at freemarker.core.EvalUtil.coerceModelToString(EvalUtil.java:355)
    在
  freemarker.core.Expression.evalAndCoerceToString(Expression.java:82)
    在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)
    在
  freemarker.core.IteratorBlock $IterationContext.executeNestedBlockInner(IteratorBlock.java:268)
    在
  freemarker.core.IteratorBlock $IterationContext.executeNestedBlock(IteratorBlock.java:220)
    在
  freemarker.core.IteratorBlock $IterationContext.accept(IteratorBlock.java:194)
    在
  freemarker.core.Environment.visitIteratorBlock(Environment.java:572)
    在
  freemarker.core.IteratorBlock.acceptWithResult(IteratorBlock.java:78)
    在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)
    在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 at
  spark.webserver.JettyHandler.doHandle(JettyHandler.java:61)at at
  org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:189)
    在
  org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
    在
  org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:119)
    在org.eclipse.jetty.server.Server.handle(Server.java:517)at
  org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:302)at at
  org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:242)
    在
  org.eclipse.jetty.io.AbstractConnection $ReadCallback.succeeded(AbstractConnection.java:245)
    在org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:95)
    在
  org.eclipse.jetty.io.SelectChannelEndPoint $2.run(SelectChannelEndPoint.java:75)
    在
  org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.produceAndRun(ExecuteProduceConsume.java:213)
    在
  org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.run(ExecuteProduceConsume.java:147)
    在
  org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:654)
    在
  org.eclipse.jetty.util.thread.QueuedThreadPool $3.run(QueuedThreadPool.java:572)
    在java.lang.Thread.run(Thread.java:745)
  
  [qtp285763673-17]错误spark.webserver.MatcherFilter –
  java.lang.IllegalArgumentException异常:
  freemarker.core.InvalidReferenceException:以下内容已经过评估
  为null或缺失:
  ==> item.lat1 [在模板“view / result.ftl”第18行第15栏]

..但是,是的,因为我打印数组,它工作正常!

  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;
}

结果.ftl:

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

最佳答案 您正在将“Bereken”类的1个实例添加到“列表”中.

所以我认为Bereken类不提供公共属性lat1,lat2,lon1,lon2或相应的getter方法.

点赞