我在 2.5.5 版中将 Freemarker 与 Spark 框架一起使用:
<dependency>
<groupId>com.sparkjava</groupId>
<artifactId>spark-core</artifactId>
<version>2.5.5</version>
</dependency>
<dependency>
<groupId>com.sparkjava</groupId>
<artifactId>spark-template-freemarker</artifactId>
<version>2.5.5</version>
<type>jar</type>
</dependency>
我正在尝试使用换行符和连字符保存文本区域并显示值,但我收到一个我不清楚的错误。
例如,要保存,我正在使用:
<textarea class="form-control" rows="5" id="listaIngredientes"
name="listaIngredientes"></textarea>
并显示我保存的内容(来自visualizar.ftl
freemarker 文件):
<p><pre>${ingrediente.listaIngredientes}</pre></p>
这适用于大多数情况,包括显示换行符。但是,如果我尝试在 textarea 中添加连字符并在某些文本之前添加一个空格,我会收到此错误(在input
或中<pre>
):freemarker.core.InvalidReferenceException.getInstance(InvalidReferenceException.java:131)
为了更清楚,我将展示一些有效的文本和一些显示错误的文本:
- 成功:
sometext
- 成功:
sometext-sometext
- 成功:
sometext- sometext
- 错误:
sometext -sometext
- 错误:
sometext - sometext
我想知道如何预防这个问题。错误并不清楚问题所在。
谢谢!
完整日志错误:
mai 21, 2017 4:02:47 PM freemarker.log._JULLoggerFactory$JULLogger error
SEVERE: Error executing FreeMarker template
FreeMarker template error:
The following has evaluated to null or missing:
==> ingrediente.listaIngredientes [in template "ingredientes/visualizar.ftl" at line 103, column 17]
----
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: ${ingrediente.listaIngredientes} [in template "ingredientes/visualizar.ftl" at line 103, column 15]
----
Java stack trace (for programmers):
----
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.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:69)
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:130)
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)
java.lang.IllegalArgumentException: freemarker.core.InvalidReferenceException: The following has evaluated to null or missing:
==> ingrediente.listaIngredientes [in template "ingredientes/visualizar.ftl" at line 103, column 17]
----
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: ${ingrediente.listaIngredientes} [in template "ingredientes/visualizar.ftl" at line 103, column 15]
----
at spark.template.freemarker.FreeMarkerEngine.render(FreeMarkerEngine.java:72)
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:130)
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.InvalidReferenceException: The following has evaluated to null or missing:
==> ingrediente.listaIngredientes [in template "ingredientes/visualizar.ftl" at line 103, column 17]
----
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: ${ingrediente.listaIngredientes} [in template "ingredientes/visualizar.ftl" at line 103, column 15]
----
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.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:69)
... 20 more
[qtp952081498-16] INFO spark.http.matching.MatcherFilter - The requested route [/ingredientes/visualizar/1030989562] has not been mapped in Spark for Accept: [text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8]