为什么我的 Atlassian 汇合需要资源包括不起作用



我正在使用 Atlassian Confluence 5.3.4

我有一个速度模板,其中包含以下行:

#requireResource("com.mycompany.confluence.plugins.my-plugin:my-resources")

然后,在我的 atlassian 插件.xml文件中,我有以下部分:

<web-resource key="my-resources" name="My Resources">
    <resource type="download" name="stylesheet.css" location="templates/css/stylesheet.css"/>
    <resource type="download" name="JavaScript.js" location="templates/js/JavaScript.js"/>
</web-resource>

我的项目结构如下:

src/main/resources/templates/css/stylesheet.css
src/main/resources/templates/js/JavaScript.js

但是,当我编译我的插件并将其安装到 Confluence 中时,没有任何效果。我的 CSS 无法呈现,浏览器找不到我的 JavaScript 函数。我什至无法在生成的HTML文件的标头中找到JavaScript和CSS声明。为什么这不起作用?

我知道

这个问题已经有几个月了,但是本周我在使用Confluence 5.7.1时遇到了同样的问题。

我的类似情况

我试图从Confluence servlet插件模块渲染Velocity模板(如我的atlassian插件.xml中所述(。我正在创建一个上下文以传递给模板,该模板只是一个新的 HashMap,并向其中添加我的自定义参数,然后渲染模板。下面 doGet 中的 servlet 代码:

Map<String, Object> contextMap = new HashMap<String, Object>();
contextMap.put("key1", "MyValue1");
contextMap.put("key2", "MyValue2");
VelocityUtils.writeRenderedTemplate(resp.getWriter(), /templates/myTemplate.vm", contextMap);

但是,这样做意味着没有 requireResource Velocimacro 实际获取资源所需的 WebResourceManager。

修复

这个问题的另一个答案是使用WebResourceManager,据我所知,它已被弃用。此修复使用捆绑在已包含在插件中的 Confluence JAR 中的未弃用类。

使用MacroUtils.defaultVelocityContext().get("webResourceManager");您可以获得com.atlassian.confluence.plugin.webresource.DefaultConfluenceWebResourceManager的实例。这就是在 Confluence 宏插件中渲染 Velocity 模板以允许#requireResource(...)工作的方法。

但是,更好的方法是使用依赖注入,方法是创建一个类型为 com.atlassian.confluence.plugin.webresource.ConfluenceWebResourceManager 的实例变量(即接口(,然后在您正在使用的类的构造函数中设置它(V2 插件首选(或通过在您的类中创建 setter 方法(用于 V1 插件(,如下所示:

public void setConfluenceWebResourceManager(ConfluenceWebResourceManager confluenceWebResourceManager) {
    this.confluenceWebResourceManager = confluenceWebResourceManager;
}

确保contextMap设置为键的String类型,值设置为Object类型,然后将confluenceWebResourceManager对象放入上下文映射中,键为"webResourceManager"。

最终代码如下所示:

Map<String, Object> contextMap = new HashMap<String, Object>();
contextMap.put("key1", "MyValue1");
contextMap.put("key2", "MyValue2");
contextMap.put("webResourceManager", confluenceWebResourceManager);
VelocityUtils.writeRenderedTemplate(resp.getWriter(), /templates/myTemplate.vm", contextMap);

这使我的资源能够得到正确使用。

我也遇到了这个问题。我有 2 个解决方案:

第一个解决方案,使用直接链接:(您必须传递$baseUrl或使用webAction中的现有,我记不清了(

<script src="$baseUrl/download/resources/com.mycompany.confluence.plugins.my-plugin:my-resources/JavaScript.js"></script>

第二种解决方案,使用 WebResourceManager

$!webResourceManager.requireResource("com.dasannetworks.vn.atlasplugins.ditto-cost-center:my-web-resources")

使用 $webResourceManager 从控制器传递,如下所示:

WebResourceManager webResourceManager = ComponentAccessor.getWebResourceManager();

最新更新