如何在 thymeleaf 中建立绝对 URL? [英] How can you build an absolute URL in thymeleaf?
问题描述
我想显示一个在运行时生成的带有参数的绝对 url.不创建页面的 href 而是使用 th:text
显示 URL.使用 Tymeleaf 执行此操作的任何简单方法(无需连接来自 #request 对象的 URL 片段,也无需使用某些 MVC 实用程序类)?
I would like to display an absolute url generated at run-time with a parameter. Not create a href to a page but display the URL using th:text
. Any simple way to do this with Tymeleaf (without having to concatenate the URL pieces from #request object and without using some MVC utility class)?
尝试 1
<p th:text="@{/myServlet(myParam=${dynamicParameter})}"/>
- 只显示部分 URL,不包括协议、端口和主机姓名.我收到 /myServlet?myParam=123
.与 th:href
的行为相同,如果您检查 <a>
,您将看到相同的 href - 在这种情况下,浏览器会通过推断协议、端口来提供帮助等等
<p th:text="@{/myServlet(myParam=${dynamicParameter})}" />
- only displays part of the URL leaving out the protocol, port and host name. I am getting /myServlet?myParam=123
. The same behavior as for th:href
, if you inspect the <a>
you will see the same href - in that case the browser helps by inferring the protocol, port and so on
尝试 2
th:text="@{__${#httpServletRequest.requestURI}__}"
- 生成不包含协议等的当前页面的相对 URI
th:text="@{__${#httpServletRequest.requestURI}__}"
- produces a relative URI of the current page that doesn't include the protocol and so on
尝试 3
th:text="@{__${#httpServletRequest.requestURL}__}"
- 这次生成包含协议、主机和 servlet 上下文的当前页面的绝对 URL.现在的问题是,当我从不同的页面显示此文本时,我的 URL 是 ...myApp/myOtherServlet
,因此我需要编辑此字符串以将 myOtherServlet
替换为 URI我想要.
th:text="@{__${#httpServletRequest.requestURL}__}"
- produces this time an absolute URL of the current page containing the protocol, host and servlet context. The problem now is when I display this text from a different page, my URL is ...myApp/myOtherServlet
so I need to edit this string to replace myOtherServlet
with the URI I want.
非 Tymeleaf 尝试 1
@Service("urlUtils")
public class UrlUtilsServiceImpl implements UrlUtilsService {
@Override
public String getAbsoluteUrlTo(final String aPath, final String param, final String value){
return ServletUriComponentsBuilder
.fromCurrentContextPath()
.path(aPath)
.queryParam(param, value)
.build().toString();
}
}
page.html:
th:text="${@urlUtils.getAbsoluteUrlTo('/myServlet', 'myParam', ${dynamicParameter})}"
问题是主机名在到达我的服务器之前可以使用别名(参见 这个).Thymeleaf+JS 溶液
The problem is the host name that can be aliased before it reaches my server (see this). Thymeleaf+JS Sollution
使用一些java脚本加上thymeleaf
Using some java script plus thymeleaf
<p id="myUrl" th:text="@{/myServlet(myParam=${dynamicParameter})}" />
<script type="text/javascript">
$(document).ready(function () {
var myUrl= $("#myUrl");
myUrl.text(window.location.origin + myUrl.text());
});
</script>
推荐答案
您可以即时连接 servlet 请求:
You can concatenate servlet request on the fly:
th:text="${#httpServletRequest.scheme}+'://'+${#httpServletRequest.serverName}+':'+${#httpServletRequest.serverPort}+@{/myServlet(myParam=${dynamicParameter})}"
或者对于 JavaScript:
Or for JavaScript:
<script type="text/javascript">
GLOBAL.serverURI = '[[${#httpServletRequest.scheme}+'://'+${#httpServletRequest.serverName}+':'+${#httpServletRequest.serverPort}+@{/myServlet(myParam=${dynamicParameter})}]]';
</script>
这篇关于如何在 thymeleaf 中建立绝对 URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!