如何在 ftl 文件中获取当前页面 url

问题描述

我的代码是这样的:

                    <li class="${properties.@R_404_6258@ormSocialAccountListLinkClass!}"><a href="http://localhost:4200/" id="zocial-${p.alias}" class="zocial ${p.providerId}"> <span>Sign in using OTP</span></a></li>
               

问题是,我需要将当前窗口 url 作为查询参数与 localhost:4200/ 一起发送。我怎么做 ?该文件为 FTL 类型。

解决方法

这可以通过使用以下方法访问 Servlet 请求对象来实现:

${springMacroRequestContext.requestUri}

如果 ftl 是 URL 为 /help 的页面,它将返回/打印 http://www.example.com/help 对象 springMacroRequestContextRequestContext 类的表示: https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/servlet/support/RequestContext.html

但是,我看到您没有使用 Spring MVC 或 Spring Boot,因此恐怕您无法使用它,那么为什么不直接使用 Javascript:

var url = window.location.href;

查看 this answer 以获取更多选项。