PDA

View Full Version : Bug in Freemark/Spring Support



egervari
Aug 22nd, 2004, 04:37 AM
I have come across a little bug (well, more of an annoyance really) where Spring does not report the true cause of the failure when a freemarker template is not parsed properly. This is a repeatable bug that I've come across at least 6 or 7 times in the last day and it can make working with Freemarker and Spring into a much frustrating endevour.

Here is a simple template that will make it fail.


<#include "/lib/rns.ftl" />

<@html title="$&#123;pageTitle&#125;>

<@rnsTransactionListing title="$&#123;tableTitle&#125;" transactions=rnsTransactions />

</@html>


Note, you don't need this exact page to make it fail. Notice that the title attribute is missing the ending quote: "${pageTitle}

This is an easy mistake to make however, this gives the following exception:



org.springframework.context.ApplicationContextExce ption&#58; Cannot load FreeMarker
template for URL &#91;core\release.transactions.page.ftl&#93;&#58; Did you specify
the correct template loader path?
at org.springframework.web.servlet.view.freemarker.Fr eeMarkerView.initApplicationContext&#40;FreeMarkerView .java&#58;136&#41;
at org.springframework.context.support.ApplicationObj ectSupport.setApplicationContext&#40;ApplicationObject Support.java&#58;79&#41;
at org.springframework.web.servlet.view.AbstractCachi ngViewResolver.loadAndConfigureView&#40;AbstractCachin gViewResolver.java&#58;123&#41;
at org.springframework.web.servlet.view.AbstractCachi ngViewResolver.resolveViewName&#40;AbstractCachingView Resolver.java&#58;78&#41;
at org.springframework.web.servlet.DispatcherServlet. render&#40;DispatcherServlet.java&#58;629&#41;
at org.springframework.web.servlet.DispatcherServlet. doService&#40;DispatcherServlet.java&#58;535&#41;
at org.springframework.web.servlet.FrameworkServlet.s ervice&#40;FrameworkServlet.java&#58;321&#41;
at javax.servlet.http.HttpServlet.service&#40;HttpServlet .java&#58;103&#41;
at com.caucho.server.dispatch.ServletFilterChain.doFi lter&#40;ServletFilterChain.java&#58;113&#41;
at com.opensymphony.module.sitemesh.filter.PageFilter .parsePage&#40;PageFilter.java&#58;129&#41;
at com.opensymphony.module.sitemesh.filter.PageFilter .doFilter&#40;PageFilter.java&#58;61&#41;
at com.caucho.server.dispatch.FilterFilterChain.doFil ter&#40;FilterFilterChain.java&#58;84&#41;
at com.caucho.server.cache.CacheFilterChain.doFilter&#40; CacheFilterChain.java&#58;211&#41;
at com.caucho.server.webapp.WebAppFilterChain.doFilte r&#40;WebAppFilterChain.java&#58;177&#41;
at com.caucho.server.dispatch.ServletInvocation.servi ce&#40;ServletInvocation.java&#58;221&#41;
at com.caucho.server.http.HttpRequest.handleRequest&#40;H ttpRequest.java&#58;263&#41;
at com.caucho.server.port.TcpConnection.run&#40;TcpConnec tion.java&#58;323&#41;
at com.caucho.util.ThreadPool.runTasks&#40;ThreadPool.jav a&#58;430&#41;
at com.caucho.util.ThreadPool.run&#40;ThreadPool.java&#58;377 &#41;
at java.lang.Thread.run&#40;Thread.java&#58;534&#41;



I assure you, the loader path is not a problem. If one fixes the problem in the template, it will work fine. However, if I re-create the problem, this is what happens:



freemarker.core.ParseException&#58; Encountered "&#123;" at line 5, column 41.
Was expecting&#58;
"=" ...

at freemarker.core.FMParser.generateParseException&#40;FM Parser.java&#58;4411&#41;
at freemarker.core.FMParser.jj_consume_token&#40;FMParser .java&#58;4286&#41;
at freemarker.core.FMParser.NamedArgs&#40;FMParser.java&#58;1 886&#41;
at freemarker.core.FMParser.UnifiedMacroTransform&#40;FMP arser.java&#58;1771&#41;
at freemarker.core.FMParser.FreemarkerDirective&#40;FMPar ser.java&#58;2216&#41;
at freemarker.core.FMParser.Content&#40;FMParser.java&#58;243 3&#41;
at freemarker.core.FMParser.OptionalBlock&#40;FMParser.ja va&#58;2601&#41;
at freemarker.core.FMParser.Root&#40;FMParser.java&#58;2773&#41;
at freemarker.template.Template.<init>&#40;Template.java&#58;144&#41;
at freemarker.cache.TemplateCache.loadTemplate&#40;Templa teCache.java&#58;376&#41;
at freemarker.cache.TemplateCache.getTemplate&#40;Templat eCache.java&#58;347&#41;
at freemarker.cache.TemplateCache.getTemplate&#40;Templat eCache.java&#58;229&#41;
at freemarker.template.Configuration.getTemplate&#40;Conf iguration.java&#58;426&#41;
at freemarker.template.Configuration.getTemplate&#40;Conf iguration.java&#58;398&#41;
at org.springframework.web.servlet.view.freemarker.Fr eeMarkerView.getTemplate&#40;FreeMarkerView.java&#58;168&#41;
at org.springframework.web.servlet.view.freemarker.Fr eeMarkerView.renderMergedTemplateModel&#40;FreeMarkerV iew.java&#58;151&#41;
at org.springframework.web.servlet.view.AbstractTempl ateView.renderMergedOutputModel&#40;AbstractTemplateVi ew.java&#58;160&#41;
at org.springframework.web.servlet.view.AbstractView. render&#40;AbstractView.java&#58;238&#41;
at org.springframework.web.servlet.DispatcherServlet. render&#40;DispatcherServlet.java&#58;644&#41;
at org.springframework.web.servlet.DispatcherServlet. doService&#40;DispatcherServlet.java&#58;535&#41;
at org.springframework.web.servlet.FrameworkServlet.s ervice&#40;FrameworkServlet.java&#58;321&#41;
at javax.servlet.http.HttpServlet.service&#40;HttpServlet .java&#58;103&#41;


As you can see, the correct exception is finally thrown. However, if I restart my server with the template problem intact, it's possible to repeat this resource-loader exception bug.

Motivation for fix? While the exceptions are working just fine if a template is already loaded, it makes debugging *new* pages while the server is running, or even old pages after a restart, very problematic. This template is trivial, however, if it were a library template with 600 lines of code, finding the bug would not be trivial. Even further, I noticed that this happens when a pages includes other templates with errors, even if the main template is syntactically correct.

Anyway, I would really appreciate it if you guys could implement a fix since I have quite a few more templates to make :P

Thanks,
Ken Egervari

davison
Aug 22nd, 2004, 12:41 PM
Thanks Ken. For future reference, better to log these in JIRA so they don't get lost in the forums. I've done this one..

http://opensource.atlassian.com/projects/spring/browse/SPR-279

davison
Aug 23rd, 2004, 08:45 AM
Note, you don't need this exact page to make it fail. Notice that the title attribute is missing the ending quote: "${pageTitle}

This is an easy mistake to make however, this gives the following exception:

side note: there are some good FTL plugins that will show such errors up in your editor/IDE. I've used the eclipse one to good effect. http://www.freemarker.org/editors.html

egervari
Aug 25th, 2004, 10:19 AM
Unfortunately I'm using IDEA and it's not supported. I still think using Freemarker without syntax highlighting is more productive and produces cleaner code than velocity, so I'll stick with it either way. Glad I could help and I'll be sure to use JIRA from now on.

Regards,
Ken