使用时模板处理错误?操作者

时间:2017-01-15 17:34:11

标签: java freemarker spark-java

我正在尝试使用report?is_odd_item查看循环中的索引是否为奇数,这会给我以下错误:

SEVERE: Template processing error: "Error parsing included template sections/reports.ftl\nError on line 4, column 42, in template sections/reports.ftl\nFound is_odd_item, expecting one of:  is_directive,  parent,  js_string,  j_string,  uncap_first,  is_transform,  number,  is_hash,  trim,  children,  has_content,  iso_ms,  xml,  iso_utc,  byte,  double,  left_pad,  json_string,  matches,  capitalize,  number_to_datetime,  contains,  size,  iso_local_h_nz,  iso_utc_ms,  iso_local_m_nz,  is_collection,  long,  default,  iso_utc_h_nz,  iso_local_ms,  is_boolean,  last_index_of,  c,  iso_utc_m_nz,  is_macro,  rtf,  iso_utc_nz,  upper_case,  node_name,  reverse,  cap_first,  url,  is_hash_ex,  iso_nz,  is_enumerable,  exists,  number_to_date,  first,  iso_local,  date,  iso,  replace,  float,  right_pad,  datetime,  node_type,  split,  iso_ms_nz,  number_to_time,  is_sequence,  iso_utc_m,  html,  ancestors,  iso_utc_h,  iso_local_ms_nz,  new,  last,  sort,  eval,  lower_case,  web_safe,  is_date,  is_string,  iso_local_nz,  word_list,  seq_last_index_of,  node_namespace,  string,  keys,  iso_m_nz,  values,  seq_index_of,  chunk,  sort_by,  iso_m,  starts_with,  substring,  index_of,  iso_h,  root,  floor,  iso_h_nz,  ceiling,  if_exists,  chop_linebreak,  iso_local_h,  length,  is_indexable,  groups,  is_node,  iso_local_m,  int,  iso_utc_ms_nz,  xhtml,  ends_with,  round,  interpret,  is_method,  namespace,  short,  seq_contains,  time,  is_number in sections/reports.ftl"

Error parsing included template sections/reports.ftl
Error on line 4, column 42, in template sections/reports.ftl
Found is_odd_item, expecting one of:  is_directive,  parent,  js_string,  j_string,  uncap_first,  is_transform,  number,  is_hash,  trim,  children,  has_content,  iso_ms,  xml,  iso_utc,  byte,  double,  left_pad,  json_string,  matches,  capitalize,  number_to_datetime,  contains,  size,  iso_local_h_nz,  iso_utc_ms,  iso_local_m_nz,  is_collection,  long,  default,  iso_utc_h_nz,  iso_local_ms,  is_boolean,  last_index_of,  c,  iso_utc_m_nz,  is_macro,  rtf,  iso_utc_nz,  upper_case,  node_name,  reverse,  cap_first,  url,  is_hash_ex,  iso_nz,  is_enumerable,  exists,  number_to_date,  first,  iso_local,  date,  iso,  replace,  float,  right_pad,  datetime,  node_type,  split,  iso_ms_nz,  number_to_time,  is_sequence,  iso_utc_m,  html,  ancestors,  iso_utc_h,  iso_local_ms_nz,  new,  last,  sort,  eval,  lower_case,  web_safe,  is_date,  is_string,  iso_local_nz,  word_list,  seq_last_index_of,  node_namespace,  string,  keys,  iso_m_nz,  values,  seq_index_of,  chunk,  sort_by,  iso_m,  starts_with,  substring,  index_of,  iso_h,  root,  floor,  iso_h_nz,  ceiling,  if_exists,  chop_linebreak,  iso_local_h,  length,  is_indexable,  groups,  is_node,  iso_local_m,  int,  iso_utc_ms_nz,  xhtml,  ends_with,  round,  interpret,  is_method,  namespace,  short,  seq_contains,  time,  is_number in sections/reports.ftl
The problematic instruction:
----------
==> include sections[section] [on line 16, column 17 in layout.ftl]
----------

Java backtrace for programmers:
----------
freemarker.template.TemplateException: Error parsing included template sections/reports.ftl
Error on line 4, column 42, in template sections/reports.ftl
Found is_odd_item, expecting one of:  is_directive,  parent,  js_string,  j_string,  uncap_first,  is_transform,  number,  is_hash,  trim,  children,  has_content,  iso_ms,  xml,  iso_utc,  byte,  double,  left_pad,  json_string,  matches,  capitalize,  number_to_datetime,  contains,  size,  iso_local_h_nz,  iso_utc_ms,  iso_local_m_nz,  is_collection,  long,  default,  iso_utc_h_nz,  iso_local_ms,  is_boolean,  last_index_of,  c,  iso_utc_m_nz,  is_macro,  rtf,  iso_utc_nz,  upper_case,  node_name,  reverse,  cap_first,  url,  is_hash_ex,  iso_nz,  is_enumerable,  exists,  number_to_date,  first,  iso_local,  date,  iso,  replace,  float,  right_pad,  datetime,  node_type,  split,  iso_ms_nz,  number_to_time,  is_sequence,  iso_utc_m,  html,  ancestors,  iso_utc_h,  iso_local_ms_nz,  new,  last,  sort,  eval,  lower_case,  web_safe,  is_date,  is_string,  iso_local_nz,  word_list,  seq_last_index_of,  node_namespace,  string,  keys,  iso_m_nz,  values,  seq_index_of,  chunk,  sort_by,  iso_m,  starts_with,  substring,  index_of,  iso_h,  root,  floor,  iso_h_nz,  ceiling,  if_exists,  chop_linebreak,  iso_local_h,  length,  is_indexable,  groups,  is_node,  iso_local_m,  int,  iso_utc_ms_nz,  xhtml,  ends_with,  round,  interpret,  is_method,  namespace,  short,  seq_contains,  time,  is_number in sections/reports.ftl
        at freemarker.core.Include.accept(Include.java:162)
        at freemarker.core.Environment.visit(Environment.java:221)
        at freemarker.core.MixedContent.accept(MixedContent.java:92)
        at freemarker.core.Environment.visit(Environment.java:221)
        at freemarker.core.IteratorBlock$Context.runLoop(IteratorBlock.java:167)
        at freemarker.core.Environment.visit(Environment.java:428)
        at freemarker.core.IteratorBlock.accept(IteratorBlock.java:102)
        at freemarker.core.Environment.visit(Environment.java:221)
        at freemarker.core.MixedContent.accept(MixedContent.java:92)
        at freemarker.core.Environment.visit(Environment.java:221)
        at freemarker.core.Environment.process(Environment.java:199)
        at freemarker.template.Template.process(Template.java:259)
        at spark.template.freemarker.FreeMarkerEngine.render(FreeMarkerEngine.java:70)
        at spark.TemplateViewRouteImpl$1.render(TemplateViewRouteImpl.java:61)
        at spark.TemplateViewRouteImpl$1.render(TemplateViewRouteImpl.java:58)
        at spark.TemplateViewRouteImpl.render(TemplateViewRouteImpl.java:86)
        at spark.http.matching.Routes.execute(Routes.java:62)
        at spark.http.matching.MatcherFilter.doFilter(MatcherFilter.java:128)
        at spark.embeddedserver.jetty.JettyHandler.doHandle(JettyHandler.java:50)
        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:308)
        at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:242)
        at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:261)
        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)
Caused by: freemarker.core.ParseException: Error on line 4, column 42, in template sections/reports.ftl
Found is_odd_item, expecting one of:  is_directive,  parent,  js_string,  j_string,  uncap_first,  is_transform,  number,  is_hash,  trim,  children,  has_content,  iso_ms,  xml,  iso_utc,  byte,  double,  left_pad,  json_string,  matches,  capitalize,  number_to_datetime,  contains,  size,  iso_local_h_nz,  iso_utc_ms,  iso_local_m_nz,  is_collection,  long,  default,  iso_utc_h_nz,  iso_local_ms,  is_boolean,  last_index_of,  c,  iso_utc_m_nz,  is_macro,  rtf,  iso_utc_nz,  upper_case,  node_name,  reverse,  cap_first,  url,  is_hash_ex,  iso_nz,  is_enumerable,  exists,  number_to_date,  first,  iso_local,  date,  iso,  replace,  float,  right_pad,  datetime,  node_type,  split,  iso_ms_nz,  number_to_time,  is_sequence,  iso_utc_m,  html,  ancestors,  iso_utc_h,  iso_local_ms_nz,  new,  last,  sort,  eval,  lower_case,  web_safe,  is_date,  is_string,  iso_local_nz,  word_list,  seq_last_index_of,  node_namespace,  string,  keys,  iso_m_nz,  values,  seq_index_of,  chunk,  sort_by,  iso_m,  starts_with,  substring,  index_of,  iso_h,  root,  floor,  iso_h_nz,  ceiling,  if_exists,  chop_linebreak,  iso_local_h,  length,  is_indexable,  groups,  is_node,  iso_local_m,  int,  iso_utc_ms_nz,  xhtml,  ends_with,  round,  interpret,  is_method,  namespace,  short,  seq_contains,  time,  is_number in sections/reports.ftl
        at freemarker.core.BuiltIn.newBuiltIn(BuiltIn.java:255)
        at freemarker.core.FMParser.BuiltIn(FMParser.java:795)
        at freemarker.core.FMParser.AddSubExpression(FMParser.java:731)
        at freemarker.core.FMParser.PrimaryExpression(FMParser.java:278)
        at freemarker.core.FMParser.UnaryExpression(FMParser.java:324)
        at freemarker.core.FMParser.MultiplicativeExpression(FMParser.java:440)
        at freemarker.core.FMParser.AdditiveExpression(FMParser.java:390)
        at freemarker.core.FMParser.RangeExpression(FMParser.java:561)
        at freemarker.core.FMParser.RelationalExpression(FMParser.java:516)
        at freemarker.core.FMParser.EqualityExpression(FMParser.java:481)
        at freemarker.core.FMParser.AndExpression(FMParser.java:590)
        at freemarker.core.FMParser.OrExpression(FMParser.java:613)
        at freemarker.core.FMParser.Expression(FMParser.java:226)
        at freemarker.core.FMParser.If(FMParser.java:1175)
        at freemarker.core.FMParser.FreemarkerDirective(FMParser.java:2377)
        at freemarker.core.FMParser.Content(FMParser.java:2623)
        at freemarker.core.FMParser.OptionalBlock(FMParser.java:2791)
        at freemarker.core.FMParser.List(FMParser.java:1262)
        at freemarker.core.FMParser.FreemarkerDirective(FMParser.java:2380)
        at freemarker.core.FMParser.Content(FMParser.java:2623)
        at freemarker.core.FMParser.OptionalBlock(FMParser.java:2791)
        at freemarker.core.FMParser.Root(FMParser.java:2963)
        at freemarker.template.Template.<init>(Template.java:171)
        at freemarker.cache.TemplateCache.loadTemplate(TemplateCache.java:447)
        at freemarker.cache.TemplateCache.getTemplate(TemplateCache.java:360)
        at freemarker.cache.TemplateCache.getTemplate(TemplateCache.java:235)
        at freemarker.template.Configuration.getTemplate(Configuration.java:578)
        at freemarker.core.Environment.getTemplateForInclusion(Environment.java:1490)
        at freemarker.core.Include.accept(Include.java:157)
        ... 32 more

代码:

<#list reports as report>
    <p><#if report?is_odd_item>left<#else>right</#if></p>
</#list>

Line 4: <#if report?is_odd_item>>
                   ^ (column 42)

其他涉及的事情?运算符也给我相同的错误,例如${var}?keep_after(".")

我正在使用Spark版本2.5.4和FreeMarker template engine for Spark版本2.3

2 个答案:

答案 0 :(得分:1)

你正在使用一些相当古老的FreeMarker版本。 {2.3}中添加了?is_odd_item。最后一个稳定版本是2.3.25-incubating(其中&#34; -cububating&#34;与稳定性无关,它是Apache孵化器政策所要求的)。

答案 1 :(得分:1)

您还可以使用item_cycle指令(自版本2.3.23

<#list reports as report >
    <p>${report?item_cycle('left', 'right')}</p>
</#list>

使代码更具可读性。

文档: http://freemarker.org/docs/ref_builtins_loop_var.html#ref_builtin_item_cycle