我正在尝试使用 FreeMarker 中的内置功能,但在某些情况下遇到问题。
为什么这有效
<#assign foo="bar" />
${foo?trim}
Run Code Online (Sandbox Code Playgroud)
但不是那个
<#assign foo>
bar
</#assign>
${foo?trim}
Run Code Online (Sandbox Code Playgroud)
?
我收到这个异常:
FreeMarker template error:
For "?trim" left-hand operand: Expected a string or something automatically convertible to string (number, date or boolean), but this has evaluated to a markup_output (wrapper: f.c.TemplateXHTMLOutputModel):
==> foo [in template "template.ftl" at line 23, column 7]
----
FTL stack trace ("~" means nesting-related):
- Failed at: ${foo?trim} [in template "faq.ftl" at line 23, column 5]
----
Java stack trace (for programmers): …Run Code Online (Sandbox Code Playgroud) 我有一个 spring boot 应用程序(版本 2.2.5.RELEASE),它打包为 jar 文件:
我正在使用 Freemarker(版本 2.3.29)生成 html 模板,请在下面找到我的配置:
@Configuration
public class EmailFreemarkerConfig {
@Primary
@Bean
public FreeMarkerConfigurationFactoryBean getFreeMarkerConfig() {
FreeMarkerConfigurationFactoryBean bean = new FreeMarkerConfigurationFactoryBean();
bean.setTemplateLoaderPath("/templates/");
return bean;
}
Run Code Online (Sandbox Code Playgroud)
请找到我的 pom.xml 文件,其中包含 ftl 文件扩展名:
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>false</filtering>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
<include>**/*.csv</include>
<include>**/*.vm</include>
<include>**/*.jpg</include>
<include>**/*.ftl</include>
</includes>
</resource>
</resources>
Run Code Online (Sandbox Code Playgroud)
当我打包 jar 时,我可以在以下路径找到该文件:BOOT-INF/classes/templates/file12.ftl
请在下面找到我生成 html 文件的代码:
@Autowired
private Configuration freemarkerConfig;
.....
try {
t = freemarkerConfig.getTemplate("file12.ftl");
html = FreeMarkerTemplateUtils.processTemplateIntoString(t, model);
} catch (IOException | TemplateException e) {
// TODO …Run Code Online (Sandbox Code Playgroud) 我一直在使用FreeMarker一段时间,但有一个明显的功能要么丢失,要么我想不出来(我希望后者!).如果你传递cfg.getTemplate()一个绝对路径,它就行不通.我知道你可以指定一个模板目录,但我负担不起,我的用例可以处理任何目录中的文件.有没有办法设置FreeMarker以任何用户期望的方式呈现绝对路径?
在我的应用程序中,所有freemarker模板都在/ templates/ftl/so中,在应用程序部署期间,我加载了一个类,我调用了一个扩展FreemarkerManager的类,并且有一个方法
Configuration configuration = super.createConfiguration(servletContext);
configuration.setDirectoryForTemplateLoading(new File("/templates/ftl/"));
Run Code Online (Sandbox Code Playgroud)
这样,当我需要加载模板文件时,我可以这样做:
ServletContext servletContext = ServletActionContext.getServletContext();
Configuration configFreemarker = (Configuration) servletContext
.getAttribute("freemarker.Configuration");
Template template = configFreemarker.getTemplate("pathToMyTemplate");
Run Code Online (Sandbox Code Playgroud)
在一个特定情况下,我需要获得一个来自完全不同路径的模板(而不是/ templates/ftl /).
我怎样才能在这个特定的时刻声明第二个目录进行模板加载而不会破坏调用旧路径的所有现有代码?我可以同时为模板加载提供2个不同的起点吗?
谢谢
是否可以使用Sping MVC JSP标记-Freemarker宏在Freemarker中输出单选按钮列表,基于绑定属性的Enum类型?我认为这可以使用<form:select>JSP中的标签,但可以使用单选按钮和Freemarker吗?
我目前正在使用Spring MVC 3.x,并使用freemarker视图解析器.
最近我一直想知道在作为响应发送回来之前将视图翻译成html所需的执行时间.如果这方面的事情很慢,我想做调音,这就是为什么我需要一些数字.
在普通的freemarker模式下,我实际上可以在它们之间执行简单的System.currentTimeMillis()来找出执行时间:
long start = System.currentTimeMillis();
// this could be slow or fast depending on the caching used
Template temp = cfg.getTemplate(ftlName);
...
temp.process(model, myWriter); // depends on the writer
System.out.printf("done in %s ms", System.currentTimeMillis() - start);
Run Code Online (Sandbox Code Playgroud)
但是,当使用spring mvc的freemaker视图渲染时,我该怎么做呢?
${str?replace("\d+", "", "r")};
Run Code Online (Sandbox Code Playgroud)
我想用来\d删除数字,但它不起作用!
但是${str?replace("[0-9]", "", "r")};工作!!!
所以,我想知道如何使用正则表达式像\d,\b,\w,等?
我需要在Liferay Portal上的Freemarker模板中比较不区分大小写的字符串.我尝试过:
<#if nav_item.getName().equalsIgnoreCase("home")>
<!-- if do -->
</#if>
Run Code Online (Sandbox Code Playgroud)
但我得到错误:
Expected hash. nav_item.getName() evaluated instead to freemarker.template.SimpleScalar on line 39, column 46 in navigation.ftl.
Run Code Online (Sandbox Code Playgroud)
如此处所示,我可以使用不区分大小写的公共标志来区分同一个字母的小写和大写变体.所以我试过:
<#if nav_item.getName()?matches("home", "i")>
Run Code Online (Sandbox Code Playgroud)
但是不起作用!我收到错误.
任何帮助表示赞赏!谢谢!
在我的代码中,我使用“ \ n”作为换行符。
建议我避免使用“ \ n”,因为对于不同的操作系统(UNIX,Windows和MAC)这是不同的,并且每个操作系统对此字符的解释都不同。
虽然我看到了预期的行为,但有人可以建议,我们在自由标记中是否有任何换行功能
freemarker ×10
java ×5
spring ×2
spring-mvc ×2
jsp ×1
liferay ×1
linux ×1
regex ×1
spring-batch ×1
spring-boot ×1
velocity ×1