我正在使用 Keycloak 发送忘记密码的电子邮件,从我在他们的文档和 FreeMarker 文档中读到的内容来看,我似乎应该能够很好地使用 HTML 标签。但是,当我在 password-rest.ftl 文件中使用它们时,它会呈现整个标签,如下所示:
<p>Some Text</p>
Run Code Online (Sandbox Code Playgroud)
而不是仅仅显示:一些文本
我发现这个(https://issues.jboss.org/browse/KEYCLOAK-681)说Keycloak只能发送纯文本电子邮件,我只是想看看是否有人确切知道,因为我发现了一些看起来像的东西HTML 标签应该可用如何阻止电子邮件在 FreeMarker 中显示为链接?
对此的任何建议或想法将不胜感激。
我有一个由两部分组成的问题,或者可能有两种不同的方法来解决这个问题。我收到一个 ISO 字符串,如 2015-11-17T17:10:24-0800。最终目标是在某些 Freemarker 生成的 HTML 中将字符串显示为 11/17/15 5:10 PM。我收到的字符串可以位于任何时区,但我总是需要在其本地时区显示该字符串,如上所示。目前,我们的代码只是获取字符串并将其传递到模板中并进行隐藏:
<#assign mydate = obj.mydate?datetime("yyyy-MM-dd'T'HH:mm:ssz")?string.short>
Run Code Online (Sandbox Code Playgroud)
这不再是好事,因为我相信 Freemarker 正在使用系统的本地时区,而现在我们获得了多个时区。我看到freemarker中有一个iso方法。所以我尝试
<#assign order_date = order.order_date?iso("yyyy-MM-dd'T'HH:mm:ssz")>
Run Code Online (Sandbox Code Playgroud)
但我不断收到错误:
For "?iso" left-hand operand: Expected a date, but this evaluated to a string
Run Code Online (Sandbox Code Playgroud)
好吧,我需要一个约会。与 Joda 合作,我尝试通过以下方式创建日期时间对象:
DateTime dateTime = ISODateTimeFormat.dateTimeNoMillis().parseDateTime("2015-11-17T17:10:24-0800");
Run Code Online (Sandbox Code Playgroud)
但这似乎也使用我的本地时区并显示 2015-11-17T20:10:24.000-05:00。我知道我可以用 withZone(...) 做,但我不知道除了 -0800 之外的区域或在字符串末尾传递的任何区域。所以我现在不知道该怎么办。哦,我无法更改收到的字符串的格式。
Freemarker 中似乎没有方便的内置功能来在模板中生成随机 UUID。
我能想到的最好办法是创建一个Freemarker 方法;在我的 Java 8 代码中,我注入了该方法以供以后在模板中使用。例子:
public String generate(Map<String, Object> data, String templateLocation) throws IOException, TemplateException {
try (StringWriter writer = new StringWriter()) {
Template template = configuration.getTemplate(templateLocation);
// UUID generation method injected in this line:
data.put("uuid", (TemplateMethodModelEx) (list) -> UUID.randomUUID());
template.process(data, writer);
return writer.toString();
}
}
Run Code Online (Sandbox Code Playgroud)
在 Freemarker 模板中,我可以使用如下方法:
${uuid()}
Run Code Online (Sandbox Code Playgroud)
在 Freemarker 中是否有更方便的生成 uuid 的解决方案?
这是我的 Maven 依赖项:
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.25-incubating</version>
<scope>test</scope>
</dependency>
Run Code Online (Sandbox Code Playgroud) import java.io.*;
import java.util.*;
import freemarker.template.*;
public class HelloFreemarker {
public static void main(String[] args)
throws IOException, TemplateException {
Configuration cfg = new Configuration();
cfg.setObjectWrapper(new DefaultObjectWrapper());
cfg.setDirectoryForTemplateLoading(new File("."));
Map<String, Object> model = new HashMap<String, Object>();
model.put("name", "World");
Template template = cfg.getTemplate("hello.ftl");
template.process(model,
new OutputStreamWriter(System.out));
}
}
Run Code Online (Sandbox Code Playgroud)
hello ${name}!
Run Code Online (Sandbox Code Playgroud)
我已经使用 freemarker 模板编写了一个 java 程序。但是当我尝试编译/构建该程序时,它显示配置错误。一条消息显示该配置已被弃用。我使用 jdk 8 和 jre 8 并使用 eclipse neon 作为我的 ide。请帮我执行该程序
我知道同样的问题已经在这里。但不幸的是它并不能帮助我解决我的问题。我的 FreeMarker 模板中有 HashSet。我想在视图上显示哈希集的大小。为此,我有以下内容:
\n <td>${repo.getRepoWords().size()}</td>\nRun Code Online (Sandbox Code Playgroud)\n方法 getRepoWords 是:
\n public HashSet getRepoWords() {\n return repoWords;\n }\nRun Code Online (Sandbox Code Playgroud)\n结果我看到了这个异常
\n\n\nFreeMarker 模板错误(DEBUG 模式;在生产中使用 RETHROW!):\n对于“.” 左侧操作数:需要一个散列,但其计算结果为\na序列(包装器:ftSimpleSequence):==> repo.getRepoWords() [在\n模板“repositories/detail.ftl”第 24 行第 23 列] - --- FTL\n堆栈跟踪(“~”表示与嵌套相关): - 失败于:\n${repo.getRepoWords().size()} [在模板“repositories/detail.ftl”\nat 第 24 行中,第 21 列] ~ 通过以下方式到达:#nested [在第 20 行第 1 列宏“page”中的模板\n“layouts/common.ftl”中] ~ 通过以下方式到达\n:@c.page title="\xd0\x93\ xd0\xbb\xd0\xb0\xd0\xb2\xd0\xbd\xd0\xb0\xd1\x8f\xd1\x81\xd1\x82\xd1\x80\xd0\xb0\xd0\xbd\xd0\xb8\xd1\ x86\xd0\xb0" [在模板\n"repositories/detail.ftl"第 2 行第 1 列] ---- Java 堆栈跟踪\n(对于程序员): ---- freemarker.core.NonHashException:[ ...\n异常消息已打印;请参阅上面...] at\nfreemarker.core.Dot._eval(Dot.java:48) at\nfreemarker.core.Expression.eval(Expression.java:83) at\nfreemarker.core.MethodCall._eval(MethodCall .java:58) at\nfreemarker.core.Expression.eval(Expression.java:83) at
\n
我可以在 FreeMarker 模板中使用 HashSet 的 …
我有2个项目。一个项目 (A) 包含我所有的核心功能,例如我的实体/服务/daos 等。它还包含许多我希望在我的另一个项目 (B) 中利用和重用的 .ftl 模板。我已经成功地让 (B) 使用了 (A) 中的类,但我在重用 freemarker 模板方面没有运气。
项目 (B) 是一个 Spring Boot 应用程序 (v2.1.3),所以我认为我使用 application.property: 是正确的:spring.freemarker.template-loader-path而不是定义一个新的@Bean.
由于是 spring-boot 应用程序,默认情况下,如果没有此属性,项目将在项目自己的src/main/resources/templates位置查找,但我的目标是让项目 (A) 没有自己的模板,并让我的控制器返回在其中找到的模板项目(B)。
在我的 Maven 依赖项中,层次结构是这样的:
projectA-0.0.1.jar
templates
folder1
exampleA.ftl
folder2
exampleB.ftl
Run Code Online (Sandbox Code Playgroud)
目前,我的控制器设置为返回return new ModelAndView("folder1/exampleA")上下文前缀为 src/main/resources/templates/ 的内容
有谁知道我需要给spring.freemarker.template-loader-path属性的值的正确格式,以便指向我的依赖 jar 中的模板而不是本地 src/main/resources/templates?
dependencies freemarker external spring-boot application.properties
将 spring-boot-parent 升级到 后2.2.0.RELEASE,我基于 freemarker 的 Spring Boot Web 应用程序无法正确处理请求。
我有一个@Controller,供应/hello用src/main/resources/templates/hello.ftl。
@Controller
class HelloController() {
@RequestMapping(value = ["/hello"])
fun hello(): String {
return "hello"
}
}
Run Code Online (Sandbox Code Playgroud)
根据要求,它只会出现在一个错误页面上,上面写着There was an unexpected error (type=Not Found, status=404).。
错误堆栈跟踪并没有说太多。它只是说org.springframework.web.servlet.resource.ResourceHttpRequestHandler: Resource not found。
我pom.xml的基本上如下:
@Controller
class HelloController() {
@RequestMapping(value = ["/hello"])
fun hello(): String {
return "hello"
}
}
Run Code Online (Sandbox Code Playgroud)
在升级到 Spring Boot 2.2.0.RELEASE 之前,它运行得非常好。
这里有什么问题?
我们想要处理模板,并尝试获取我们之前在standalone.xml 文件中设置的系统属性,如下所示。
\n\n</extensions>\n<system-properties>\n <property name="testProp" value="TestVal"/>\n</system-properties>\nRun Code Online (Sandbox Code Playgroud)\n\n在Keycloak的文档中,它的描述如下。
\n\n但对我们来说没有任何作用。我们总是收到以下错误消息 \xe2\x80\x9c发生内部服务器错误\xe2\x80\x9d。\n获取 Freemarker 模板中的系统属性和环境变量的正确方法是什么?
\n我想en在登录主题的 freemaker 模板中输出当前语言的两个字母语言代码 (fe )。
我已经尝试过${locale.current},但这仅输出当前语言的名称,而不输出代码。
<#assign reasonValue="xxx.ftl">
Run Code Online (Sandbox Code Playgroud)
我称之为:
<#include "${reasonValue}">
Run Code Online (Sandbox Code Playgroud)
我输出如下:
Rejected - Something
Run Code Online (Sandbox Code Playgroud)
我现在怎样才能在这个输出上使用split,因为我想得到的只是Something输出
我试过了:
<#list "${reasonValue}"?split("-") as sValue>
${sValue}
</#list>
Run Code Online (Sandbox Code Playgroud)
但问题是,我得到ftl文件的名称而不是真正的价值...
freemarker ×10
java ×5
keycloak ×3
spring-boot ×2
dependencies ×1
external ×1
html-email ×1
iso8601 ×1
jboss ×1
jodatime ×1
kotlin ×1
spring ×1
themes ×1
uuid ×1
wildfly ×1