Java `name`作为freemarker中的变量名
我在ftl中有以下代码:Java `name`作为freemarker中的变量名,java,maven,freemarker,spark-java,Java,Maven,Freemarker,Spark Java,我在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&
<#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" />
插件如下所示:
<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>
src/main/resources
真的
org.apache.maven.plugins
maven编译器插件
1.8
1.8
org.codehaus.mojo
execmaven插件
1.1
com.example.foo.foo-test
我的控制器如下所示:
.....
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());
。。。。。
导入spark.ModelAndView;
进口火花,火花;
导入spark.template.freemarker.FreeMarkerEngine;
......
Spark.get(“/foo”,(请求、响应)->{
Map attributes=newhashmap();
返回新的ModelAndView(属性“test.ftl”);
},新的FreeMarkerEngine());
非常奇怪。。。它应该有用,对我也有用。我怀疑有什么东西在FreeMarker解析模板之前搜索并替换模板中的${name}
(例如,customTemplateLoader
-s可以这样做)。比如,如果您编写${name}
而不是${name}
,会发生什么情况?这是因为您已经配置了maven来过滤您的资源,它会用项目名替换${name}
占位符
从pom中删除
,或者如果确实需要资源筛选,可以排除ftl-s文件
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
src/main/resources
真的
顺便说一句,
maven资源插件
文档关于使用${name}
占位符演示了这种行为 你在使用maven吗?@AleksandrM是的,这与此有关吗?你如何在maven中打包/包括ftl-s?你能展示你的pom.xml吗?@AleksandrM我刚刚用信息show your pomplugins
部分更新了这个问题。你说得对。如果我写${name}
,它就会打印我想要的东西。。但是,如果仅在第一行打印${name},则会抛出一个错误,即名称未定义。。为什么这只发生在宏内部?我不知道在FreeMarker获得模板文件之前,模板文件上应用了什么魔法。。。你能做一个System.out.println(template)
其中template
是fremarker.template.template
对象来查看模板中的内容吗?还有,那是什么FreeMarker版本(${.version}
)?我希望不是10年前的预览版…或者更确切地说,System.out.println(template.getSource())
。版本打印2.3.19
,对于模板,我不知道从哪里获得模板,因为它嵌入了spark模板freemarker中。2.3.19是一个过时但在其他方面稳定的版本,所以这不是问题所在。至于如何获得模板
对象,您可以做一个hack:实现模板定向模型
,在那里您可以从env.getTemplate()
(至少如果它是顶级模板),并将结果打印到env.getOut()
Writer
。然后在模板中调用此指令,如
。就是这样!真不敢相信Maven会这么做。。我不明白为什么只发生在宏内部。。谢谢
.....
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());
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>