Spring mvc 中使用ftl引用共通文件出错 FreeMarker template error: Error reading included file "/WEB-INF/ftl/common/errormessage.ftl"

时间:2022-02-23 14:39:13

初次接触spring mvc,想做一个小的练习项目,结果在ftl文件中引用其它的共通ftl文件时出错。

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAANwAAADcCAIAAACUOFjWAAAPt0lEQVR4nO2db2gcZR7HJ68ORF/ctbGcULg7CsfdCS1ViregKS1tVLR/Dt8IgoiyWrB4JA1K2xgxBkPT0FUbIkj1rBwN2kAtZSHFwpVC7IsiRRIoXdFoxdbYhBBtu7spfe7FbHafeeZ5nnl2599vn/1+eNBkdp6ZJ7ufPjM7O9/9Oawx7pSmhtrL16fZ5Bo21j6xu126Vj7rONl8g7uIn3zWcTK5QtrDAAJOg/3ulNhkx8TudrexyQ7ZSoVchvSLDilp0qiUJpB/zckPsEWJRcpCLuM4jkP60M0YpKRKnDMlAA0BKQE5ICUgB6QE5ICUgByQEpADUgJyQEpADiMpzw1tqLaYxpHP+q61F3IZ8dp2IZeprFW9Pl/F7ZzPehZqLo3X1szkCtUNipf7a3vU7BRES7CU4y+v0fwaGb5PVyoG8Ms4Q1Qfq3s2I9FaubLiUyi/lPAwdgKkPDewoXjt/eK19xcu5a5dHJz532uXTvz73MAGvkU0EsHKQi7jZHO5DC8Bt4qRlEG3KYlSZrLZjKAlpEyBACm/GFhfXDivaV8MrNduYHKid9eFWZORCNNaPutkcgWPVD6HAqUMsEiyQXfCrHWBlCkQJGXf+uK1E5r24bN/CtjD9IGR3s6R3s6xs1f0K3pectfJ2v+Z5MgcKGXQ7ZxSy71aQsoUCJDyVN/64sx/qm28a62mabc0OdHbOdLbOXJ4bF61CveaV3WqyefV0Peeo+KK+Rsdppx6eS31b3QgaCwESDne9Y/ipZzbxrvWFmfed9vOf672N4PdXblwWONlVQxuilteJsxSdRy+Pap5XFWeD9S0xEyZAgFS/vflNcVLg8VLg+Nda90f3PbYA/c99dC6px5aVy5/5f7w2IP36TY0OzYW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0SnLSxjYpJZCK2MJJA1pAylShWSaWOJAydjwnnJgkDYCUgByQEpADUgJyQEpADkgJyAEpATkgJSAHTSkbrokJbCBeKc8Nbai2evpBypbm/+BjhMnauZ9pAAAAAElFTkSuQmCC" alt="" />

目录结构如图所示,想在login.ftl中引用common下的errormessage.ftl

<#include '/WEB-INF/ftl/common/errormessage.ftl' />

结果画面报错:

FreeMarker template error: Error reading included file "/WEB-INF/ftl/common/errormessage.ftl": Template "/WEB-INF/ftl/common/errormessage.ftl" not found. The failing instruction (FTL stack trace): ---------- ==> #include "/WEB-INF/ftl/common/errorme... [in template "home/login.ftl" at line 26, column 13] ---------- Java stack trace (for programmers): ---------- freemarker.core._MiscTemplateException: [... Exception message was already printed; see it above ...] at freemarker.core.Include.accept(Include.java:168) at

…………

怀疑是路径问题或是拼写错误,反复修改,病急乱投医,甚至将errormessage.ftl拷贝到login.ftl所在的目录下,用 'errormessage.ftl','/errormessage.ftl','./errormessage.ftl'等不断尝试,不得解。

最后到处检查各种配置文件,发现在spring-mvc.xml文件中配置视图解析器时,指定过模板的加载路径:


    <!-- 配置freeMarker的模板路径 -->
<bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="configLocation" value="classpath:freemarker.properties" />
<property name="templateLoaderPath" value="/WEB-INF/ftl/" />
<property name="freemarkerVariables">
<map>
<entry key="xml_escape" value-ref="fmXmlEscape" />
</map>
</property>
</bean>

猜想在ftl中引用其它文件时也会受此影响,于是将<#include '/WEB-INF/ftl/common/errormessage.ftl' /> 修改为<#include 'common/errormessage.ftl' />,问题解决。