我有一个具有布尔属性 letterActivated 的 Customer 对象。getter 方法按照标准 java 实践如下。
public Boolean isLetterActivated() {
return letterActivated;
}
Run Code Online (Sandbox Code Playgroud)
但似乎 Freemaker 没有拾取customer.letterActivated表达式,我想这可能是因为我没有 getLetterActivated 方法。(请注意,客户对象的其他表达式工作正常,它们都有 get*** 方法)。
我应该如何从 FTL 引用这个布尔变量?请注意,我无法更改 getter 方法名称 (isMobileBillingAccount),因为它在现有代码中并在许多地方引用。我猜这也是布尔吸气剂的正确方法。
这是我们的用例。我们正在从数据库加载 freemarker 语法并对其进行处理。我们正在处理近一百万条记录。一切正常。但是当我分析应用程序时,我发现我的 freemarker 处理方法是瓶颈,并且花费了大部分时间。阅读了 freemarker 文档后,我得到了一些关于我的问题的提示。每次我进行处理时,我都会创建新的 freemarker.template.Template 对象(创建它似乎很昂贵)。我找不到这样做的正确/更有效的方法。
Run Code Online (Sandbox Code Playgroud)public FTLTemplateEngine() { cfg = new Configuration(); } public String process(String template, Map<String, Object> input) throws IOException, TemplateException { String rc = null; final Writer out = new StringWriter(); try { final Template temp =new Template("TemporaryTemplate", new StringReader(template), cfg); temp.process(input, out); } catch (InvalidReferenceException e) { log.error("Unable to process FTL - " + template); throw new InvalidReferenceException("FTL expression has evaluated to null or it refers to something that doesn't exist. - …
FreeMarker 是否支持 Java 8 中的 Optional 值?
例如,我有 String id,它的 getter 方法是这样的:
public Optional<String> getId() {
return Optional.ofNullable(Id);
}
Run Code Online (Sandbox Code Playgroud)
我将如何在 .ftl 文件中引用它。似乎 {data.id} 找不到正确的 Optional 值。但给了我 Optional[1334586]
我第一次尝试使用 Spring Boot 显示 Freemarker 视图,目前在浏览器中出现以下错误:
白标错误页面
此应用程序没有明确的 /error 映射,因此您将其视为后备。
Sun Feb 19 17:59:07 GMT 2017 出现意外错误(类型=未找到,状态=404)。没有可用的消息
我正在使用 Spring Boot 1.5。
我的文件结构:
登录控制器
package com.crm.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.crm.service.LoginService;
@Controller
public class LoginController {
@Autowired
private LoginService loginService;
@RequestMapping("/login")
public String authenticate() {
return "loginPage";
}
}
Run Code Online (Sandbox Code Playgroud)
应用程序属性
spring.freemarker.template-loader-path: /
spring.freemarker.suffix: .ftl
Run Code Online (Sandbox Code Playgroud)
服务器
package com.crm;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Server{
public static void main(String[] args) {
SpringApplication.run(Server.class, args);
}
}
Run Code Online (Sandbox Code Playgroud)
为什么 Spring 无法解析 …
我正在使用 Freemarker 版本2.3.20。
我有一个数据结构,其中包含两个日期 - 一个在本地时间,一个在UTC 时间。
// 2017-07-17 18:30 UTC
ZonedDateTime utcTime = ZonedDateTime.of(2017, 7, 17, 18, 30, 0, 0, ZoneId.of("UTC"));
// 2017-07-17 20:30 (+02:00)
ZonedDateTime localTime = utcTime.withZoneSameInstant(ZoneId.of("Europe/Berlin"));
Run Code Online (Sandbox Code Playgroud)
Freemarker 只能处理java.util.Date所以我要交出日期。
Map<String, Object> mapping = new HashMap<String, Object>();
mapping.put("departureTimeLocal", Date.from(localTime.toInstant()));
mapping.put("departureTimeUtc", Date.from(utcTime.toInstant()));
Run Code Online (Sandbox Code Playgroud)
在我的模板中,我希望编写如下内容:
Departure (local): ${departureTimeLocal?string['HH:mm']}
Departure (UTC) : ${departureTimeUtc?string['HH:mm']}
Run Code Online (Sandbox Code Playgroud)
因此,我希望看到:
Departure (local): 20:30
Departure (UTC) : 18:30
Run Code Online (Sandbox Code Playgroud)
我目前看到的是:
Departure (local): 20:30
Departure (UTC) : 20:30 <#-- timestamp interpreted in local …Run Code Online (Sandbox Code Playgroud) 我有用 Java 编写的 Web 应用程序。它使用 Spring 和 Freemarker 从模板制作电子邮件,并使用 Spring JavaMailSender 发送它们。
我发送的电子邮件在其内容中包含时间。问题是服务器将时间存储在 UTC+00 时区,而客户端可能有不同的时区,例如 UTC+03。例如,在电子邮件内容中有 20/07/2017 11:30 (UTC+00),但收件人预计 20/07/2017 14:30 (UTC+03)。
我的问题是:是否可以在客户端时区的电子邮件内容中显示时间,而无需在服务器端了解他的时区信息?例如,是否有一些技巧可以告诉电子邮件客户端在他的时区解释给定的时间?
上下文:使用https://www.keycloak.org/作为身份验证提供程序。
我有一个用例,我需要根据登录页面中查询字符串参数的值呈现 HTML 块。该值将来自与此类似的登录 URL:
http://localhost:8080/auth/realms/default/protocol/openid-connect/auth?...&customvar=1
Run Code Online (Sandbox Code Playgroud)
我想customvar有空login.ftl来完成我的目标。到目前为止,我已经尝试从客户端 bean 中检索 baseUrl,但它不可用,当我尝试访问 baseUrl 时应用程序崩溃。我还尝试访问 中的请求 url ${url},但它也不可用。
在此先感谢您的帮助。
在 Android Studio 中,我尝试按照以下步骤创建新的 Java 模板
Android Studio --> File --> Edit File Template --> Added New file with java extension.--> 添加了所需的代码
我可以使用这个模板来创建新文件。但我想看看这个模板路径。这些在哪里?
我可以在 Android Studio 中看到预定义模板,/AndroidStudio/plugin/android/lib/templates但看不到我新创建的模板。任何人都知道在哪里可以找到新创建的模板。?
谢谢
我一直在Magnolia 代码示例中的 FreeMarker 代码末尾看到感叹号。例如:
${content.header!}
Run Code Online (Sandbox Code Playgroud)
感叹号叫什么,它有什么作用?
freemarker ×10
java ×6
keycloak ×2
spring ×2
android ×1
date ×1
email ×1
gmail ×1
jakarta-ee ×1
java-8 ×1
magnolia ×1
optional ×1
spring-boot ×1
spring-mvc ×1