Spring boot 如何自动转义thymeleaf pathvariable中的特殊字符?

Spring boot 如何自动转义thymeleaf pathvariable中的特殊字符?,spring-boot,thymeleaf,path-variables,Spring Boot,Thymeleaf,Path Variables,我正在从thymeleaf href模板向Spring引导控制器发送一个字符串pathvariable,但如果此字符串中包含URL,则其斜杠字符“/”将被解释为href URL的一部分。有没有办法让这些符号自动转义 百里香 <a th:href="@{/displayComments/{comments}(comments=${excerpt.comments})}">Comments</a> URL报告为出现意外错误(type=notfound,s

我正在从thymeleaf href模板向Spring引导控制器发送一个字符串pathvariable,但如果此字符串中包含URL,则其斜杠字符“/”将被解释为href URL的一部分。有没有办法让这些符号自动转义

百里香

<a th:href="@{/displayComments/{comments}(comments=${excerpt.comments})}">Comments</a>
URL报告为
出现意外错误(type=notfound,status=404)


ThymileAF将自动对查询参数进行URL编码(在
之后的内容)

但在这里,您希望对URL路径段进行URL编码,因此需要显式处理:

th:href="@{/displayComments/{comments}(comments=${#uris.escapePathSegment(excerpt.comments, 'UTF-8')})}" 

我建议使用带有编码参数的函数版本,这样您就可以显式地使用
UTF-8
,避免被意外的默认编码绊倒。

您看到了吗?似乎这样应该可以工作:
th:href=“@{/displayComments/{comments}(comments=${uri.escapePathSegment(extract.comments)})}“
(未经测试)我建议使用:
th:href=“@{/displayComments/{comments}(comments=${uris.escapePathSegment(extract.comments,'UTF-8')}”)
-它显式地包括编码为
UTF-8
,这样你就可以避免被意外的默认编码绊倒。@WimDeblauwe嘿,谢谢你。。。这有帮助
http://localhost:8080/displayComments/https://www.youtube.com/watch%3Fv=j1wgaFJ0750
th:href="@{/displayComments/{comments}(comments=${#uris.escapePathSegment(excerpt.comments, 'UTF-8')})}"