在Freemarker中如何从相对链接获取绝对链接

3
我正在使用Magnolia - CMS创建一个网站。现在我正在实现一个博客页面,在每个博客页面上都有几个分享按钮。现在我正在忙于实现Twitter分享按钮。在这里,我将使用Twitter卡片。为此,我需要在元标记中提供图像的URL。主要问题是:我像这样检索我的图像:${damfn.getAssetLink(content.blogImage)}。这只返回到我的资源的相对路径。有没有一种快速的方式(在freemarker中),可以将其转换为绝对链接?
非常感谢!

1
哪个版本的Magnolia?看最新的DAM,它应该返回绝对URI,所以你只需要添加域名和协议。 - Jan
我使用的是4.5版本。 - Kornelito Benito
1
如果仍然打开,则应在图像(相对)URL之前添加${ctx.contextPath} - Jozef Chocholacek
添加上上下文路径仍然不能使其成为绝对链接。 - Manticore
不是的。这是真的。context path 只会添加应用程序部署的 contextpath。在我的情况下,它大多数时候是 ROOT,所以它并没有做什么。 - Kornelito Benito
1个回答

1
通常情况下,您需要在magnolia.properties文件中定义,然后可以使用Components.getComponent(ServerConfiguration.class).getDefaultBaseUrl()来检索它。
现在,您需要将服务安装到freemarker中。您可以通过在启动时将installer-tasks添加到渲染器中来完成此操作。您需要在module-version-handler中覆盖getStartupTasks(...),类似于以下内容:
@Override
protected List<Task> getStartupTasks(InstallContext installContext) {
    final List<Task> tasks = new ArrayList<>();
    tasks.add(new InstallRendererContextAttributeTask("rendering", "freemarker", "serverConf", ServerConfiguration.class.getName()));
    tasks.add(new InstallRendererContextAttributeTask("site", "site", "serverConf", ServerConfiguration.class.getName()));
    return tasks;
}

现在你可以在 Freemarker 中调用它:
"${serverConf.defaultBaseUrl}/${ctx.contextPath}/${damfn.getAssetLink(content.blogImage)}"

请检查斜杠是否必要,并确保您的Magnolia配置中正确设置了defaultBaseUrl(“/ server / ...”)。
编辑:通过在freemarker中调用当前请求,应该更容易一些${Request},因此它可以是"${Request.domain}/${ctx.contextPath}/${damfn.getAssetLink(content.blogImage)}",而无需将serverConfiguration注入渲染器。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接