如何使用 Thymeleaf 设置包含与号的 URL? [英] How do I set a URL that includes an ampersand with Thymeleaf?
问题描述
我有类似的东西:
Locale defaultLocale = Locale.getDefault();
final Context ctx = new Context(defaultLocale);
String url = getHost() + "/page?someId=" + some.getId() + "&someParam=" + Boolean.TRUE;
ctx.setVariable("url", url);
final String htmlContent = templateEngine.process("theHtmlPage", ctx);
但是当我查看生成的 HTML 以打印 url
时,它会在 URL 中显示 &
而不是 &
.
But when I look at the resulting HTML to print url
, it shows &
instead of &
in the URL.
有什么建议吗?
我尝试使用反引号来转义 Java 代码中的&符号,但它也只是打印了这些符号.环顾四周,但没有找到太多相关的内容.也试过 &
I tried using backticks to escape the ampersand in the Java code, but it just printed those too. Looked around on SO, but didn't find much that was relevant. Also tried &
更新:好的,这不会破坏链接,但是如果没有它,Spring 似乎不会将参数someParam"解析为 true.
Update: Ok, this won't break the link, but Spring doesn't seem to resolve the parameter "someParam" as true without it.
渲染标签:
<span th:utext="${url}"></span>
输出:
<span>http://localhost:8080/page?someId=1&someParam=true</span>
推荐答案
Thymeleaf 最近有一个问题编码转义,已在 2.1.4 中修复.
Thymeleaf had a recent issue with encoding escapes, which has been fixed in 2.1.4.
这篇关于如何使用 Thymeleaf 设置包含与号的 URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!