3

How can I get the URL of the asset directory so it can be used in JavaScript to dynamically load assets?

Background:
In Tapestry 5 the path to assets can change to avoid caching issues. The path depends on the application's version and therefore usually is also different for development, test and production mode.

Injecting assets and getting their path in Java is easy. In .tml template files it's possible to include the URL using ${asset:classpath:/com/example/myApp/img/test.png}. In CSS relative paths are working fine. The best solution for JavaScript seems to be to include a script tag in a template and provide a global property that contains the path.

The problem is, how do I get the assets base URL in tapestry?
How can I access this path with JavaScript?

4

2 回答 2

3

看看这里的讨论这里的 JIRA 。

Thiago H. de Paula Figueiredo创建了一个 RequestFilter 来解决这个问题,以便他可以使用wymeditor在 javascript 中加载动态相关资产。源代码在这里

* 编辑 *以上评论指的是最新(未发布)5.4 版挂毯。

对于低于 5.4 的版本,我假设您可以通过以下方式获取根类路径资产 URL:

${asset:classpath:/}

或者

@Inject
private AssetSource assetSource;

public String getRootPath() {
    return assetSource.getClasspathAsset("/").toClientURL();
}
于 2013-10-14T08:48:17.553 回答
0

也许这会起作用:

@Inject @ClasspathProvider
private AssetFactory classpathAssetFactory;

public String getClasspathRootUrl() {
    Resource classpathRoot = classpathAssetFactory.getRootResource();
    return classpathAssetFactory.createAsset(classpathRoot).toClientURL();
}
于 2013-10-14T14:23:19.760 回答