在Freemarker中,`name`作为变量名会中断 [英] `name` as a variable name in freemarker breaks
问题描述
我在ftl中有以下代码:
I have the following code in an ftl:
<#macro field label name value="" type="text">
${name}
${name!"print if null"}
<div class="field">
<div class="clearfix" id="${name}_field">
<label for="${name}">${label}</label>
<div class="input">
<input type="${type}" id="${name}" name="${name}" value="${value}">
<span class="help-inline"></span>
<span class="help-block"></span>
</div>
</div>
</div>
</#macro>
<@field label="label" name="test" />
这是打印此内容:
foo-test
test
<div class="field">
<div class="clearfix" id="foo-test_field">
<label for="foo-test">label</label>
<div class="input">
<input type="text" id="foo-test" name="foo-test" value="">
<span class="help-inline"></span>
<span class="help-block"></span>
</div>
</div>
</div>
foo-test
是我的应用程序的名称,但无法理解为什么将其打印在其中..只是使用ctrl + f搜索foo-test
,并且在ftl或控制器中均无处...
foo-test
is the name of my app but can't understand why is it being printed there.. Just used ctrl+f to search for foo-test
and it is nowhere in the ftl or the controller...
除此以外,让我们假设name
是一个具有我的应用程序名称的变量.那么,为什么第二张打印仅打印我传递给宏的正确值?这真的很奇怪...
Besides this, let's suppose that name
is a variable that has the name of my app.. Then why the second print just prints the right value that i passed to my macro?? This is really strange...
我使用Maven和spark,所以我有这种依赖性:
I use Maven and spark so I have this dependency:
<dependency>
<groupId>com.sparkjava</groupId>
<artifactId>spark-template-freemarker</artifactId>
<version>2.0.0</version>
</dependency>
插件是这样的:
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.1</version>
<configuration>
<mainClass>com.example.foo.foo-test</mainClass>
</configuration>
</plugin>
</plugins>
</build>
我的控制器如下所示:
.....
import spark.ModelAndView;
import spark.Spark;
import spark.template.freemarker.FreeMarkerEngine;
......
Spark.get("/foo", (request, response) -> {
Map<String, Object> attributes = new HashMap<>();
return new ModelAndView(attributes, "test.ftl");
}, new FreeMarkerEngine());
推荐答案
之所以会发生这种情况,是因为您已经配置了maven来过滤您的资源,它确实用项目名称替换了${name}
占位符.
This is happening because you have configured maven to filter your resources, which it does substituting ${name}
placeholder with the name of your project.
从pom中删除<resources>
,或者如果您确实需要资源过滤,则可以排除ftl-s文件.
Remove <resources>
from your pom or if you do need resource filtering you can exclude ftl-s files.
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
关于资源过滤maven-resources-plugin文档>使用${name}
占位符精确演示了此行为. :)
BTW the maven-resources-plugin
documentation about resource filtering demonstrates exact this behaviour with the ${name}
placeholder. :)
这篇关于在Freemarker中,`name`作为变量名会中断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!