我有一个SPRING应用程序直接在Java或Tomcat 7服务器上运行(使用spring启动)。我需要为几页(映射)创建绝对URL,以便可以通过电子邮件发送这些链接。我需要使用Thymeleaf为我的SPRING应用程序创建绝对URL
我认为这很简单,但现在看起来很难。我更喜欢100%写在Thymeleaf上的解决方案,但如果这是不可能的,我当然可以向Thymeleaf提供来自我的Java代码的一些变量。
任何人在Thymeleaf解决过这个问题吗?
我有一个SPRING应用程序直接在Java或Tomcat 7服务器上运行(使用spring启动)。我需要为几页(映射)创建绝对URL,以便可以通过电子邮件发送这些链接。我需要使用Thymeleaf为我的SPRING应用程序创建绝对URL
我认为这很简单,但现在看起来很难。我更喜欢100%写在Thymeleaf上的解决方案,但如果这是不可能的,我当然可以向Thymeleaf提供来自我的Java代码的一些变量。
任何人在Thymeleaf解决过这个问题吗?
在我看来,你应该在属性文件中提供一个服务器URL,并且在Thymeleaf中访问它。您可以通过在一个视图访问的Spring bean做到这一点:
<div th:text="${@urlService.getApplicationUrl()}">...</div>
在上面的例子中,urlService
是一个Spring bean。
请参阅这样的回答:https://stackoverflow.com/a/675903/718515
您可能也有兴趣在#ctx
变量,使您可以访问servletContext
。见Thymeleaf的文章:http://www.thymeleaf.org/doc/html/Using-Thymeleaf.html#base-objects
我希望它有帮助!
我刚刚做了一些类似的事情,我使用Thymeleaf来生成HTML电子邮件(当然,链接必须是绝对的)。我使用Thymeleaf的内置@{}
链接语法来创建相对于服务器的相应URL(因为它调用HttpServletResponse.encodeURL()
,我需要做一个自定义实现来完成一些额外的URL调用),然后使用Spring的ServletUriComponentsBuilder来制作使用HttpServletRequest服务器信息的绝对URL。
<p th:with="
re[email protected]{|/my/path/${customer.code}/info/|},
customerInfoPath=${T(org.springframework.web.servlet.support.ServletUriComponentsBuilder).fromContextPath(#httpServletRequest).replacePath(relativeCouponPath).toUriString()}">
Go see your info at
<<a th:href="${customerInfoPath}" th:text="${customerInfoPath}">Link</a>>.
</p>
有可能是一个更好的办法,但这个工作很适合我和做的完全URL内Thymeleaf绝对的决策(尽管使用Spring的库)。
今天我做了一个整合,建议使用完整的URL来兼容。
基座应该改变根据环境(I只是把它作为属性)。
最后一个@ {/ js/i/dyn}是获取contextPath。
<script th:src="${'https://' + base} + @{/js/i/dyn}"></script>
裁判:http://www.thymeleaf.org/doc/articles/standardurlsyntax.html
到Thymeleaf文档的链接已经死了。 – Dragomok