如何设置包含 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.[=18 中显示 &
而不是 &
=]
有什么建议吗?
我尝试使用反引号来转义 Java 代码中的 & 符号,但它也只是打印了那些。环顾四周,但没有找到太多相关的东西。也试过&
更新:好的,这不会破坏 link,但是 Spring 似乎无法在没有它的情况下将参数 "someParam" 解析为 true。
渲染标记:
<span th:utext="${url}"></span>
输出:
<span>http://localhost:8080/page?someId=1&someParam=true</span>
Thymeleaf had a recent issue with encoding escapes,已在 2.1.4 中修复。
最好使用专用的thymeleaf link url syntax。
如果你想用两个参数构造 and url 并将其设置为 href
属性,你可以这样做:
<a th:href="@{page(param1 = ${param1}, param2 = ${param2})}">link</a>
生成的html将是:
<a href="page?param1=val1&param2=val2">link</a>
浏览器会请求:
page?param1=val1¶m2=val2
=== 编辑 ===
为了回答对多巴曲曼的反对意见,我刚刚(再次)测试了我的答案并且效果很好。
在我的回答中,thymeleaf 自动添加了用作参数分隔符的符号。这个添加的符号是 html 实体编码,由 thymeleaf 存储在 html.
如果您在 param1 或 param2 中有另一个符号,这个符号应该是 html 在百里香模板 中编码的实体。但是在生成的html.
中会出现百分比编码
示例(使用 thymeleaf 2.1 测试。5.RELEASE):
param1
的值为 abc
,param2
的值为 12&3
在 thymeleaf 模板中,所有 & 符号必须编码为 html 实体,我们有:
<a th:href="@{page(param1 = ${'abc'}, param2 =${'12&3'})}">link</a>
在生成的 html 中,用作参数分隔符的 & 符号被编码为 html 实体,param2 值中的 & 符号由 thymeleaf 进行百分比编码:
<a href="page?param1=abc&param2=12%263">link</a>
当你点击link时,浏览器会解码html实体编码而不是百分比编码,地址栏中的url将是:
<a href="page?param1=abc&param2=12%263">link</a>
通过wireshark查询,我们从HTTP请求中得到:
GET /page?param1=abc¶m2=12%263
为避免此类问题而不是“&”符号,您可以为该符号使用 UTF 代码,例如,如果是 UTF-8,请使用“\u0026”。
我有类似的东西:
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.[=18 中显示 &
而不是 &
=]
有什么建议吗?
我尝试使用反引号来转义 Java 代码中的 & 符号,但它也只是打印了那些。环顾四周,但没有找到太多相关的东西。也试过&
更新:好的,这不会破坏 link,但是 Spring 似乎无法在没有它的情况下将参数 "someParam" 解析为 true。
渲染标记:
<span th:utext="${url}"></span>
输出:
<span>http://localhost:8080/page?someId=1&someParam=true</span>
Thymeleaf had a recent issue with encoding escapes,已在 2.1.4 中修复。
最好使用专用的thymeleaf link url syntax。
如果你想用两个参数构造 and url 并将其设置为 href
属性,你可以这样做:
<a th:href="@{page(param1 = ${param1}, param2 = ${param2})}">link</a>
生成的html将是:
<a href="page?param1=val1&param2=val2">link</a>
浏览器会请求:
page?param1=val1¶m2=val2
=== 编辑 ===
为了回答对多巴曲曼的反对意见,我刚刚(再次)测试了我的答案并且效果很好。
在我的回答中,thymeleaf 自动添加了用作参数分隔符的符号。这个添加的符号是 html 实体编码,由 thymeleaf 存储在 html.
如果您在 param1 或 param2 中有另一个符号,这个符号应该是 html 在百里香模板 中编码的实体。但是在生成的html.
中会出现百分比编码示例(使用 thymeleaf 2.1 测试。5.RELEASE):
param1
的值为 abc
,param2
的值为 12&3
在 thymeleaf 模板中,所有 & 符号必须编码为 html 实体,我们有:
<a th:href="@{page(param1 = ${'abc'}, param2 =${'12&3'})}">link</a>
在生成的 html 中,用作参数分隔符的 & 符号被编码为 html 实体,param2 值中的 & 符号由 thymeleaf 进行百分比编码:
<a href="page?param1=abc&param2=12%263">link</a>
当你点击link时,浏览器会解码html实体编码而不是百分比编码,地址栏中的url将是:
<a href="page?param1=abc&param2=12%263">link</a>
通过wireshark查询,我们从HTTP请求中得到:
GET /page?param1=abc¶m2=12%263
为避免此类问题而不是“&”符号,您可以为该符号使用 UTF 代码,例如,如果是 UTF-8,请使用“\u0026”。