Question

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?

Was it helpful?

Solution

Take a look at the discussion here and the JIRA here.

Thiago H. de Paula Figueiredo has created a RequestFilter to workaround this issue so that he can use wymeditor which loads dynamic relative assets in javascript. Source code here

* edit * The above comments refer to latest (unreleased) version 5.4 of tapestry.

For versions less than 5.4 I assume you can get the root classpath asset URL via:

${asset:classpath:/}

Or

@Inject
private AssetSource assetSource;

public String getRootPath() {
    return assetSource.getClasspathAsset("/").toClientURL();
}

OTHER TIPS

Perhaps this will work:

@Inject @ClasspathProvider
private AssetFactory classpathAssetFactory;

public String getClasspathRootUrl() {
    Resource classpathRoot = classpathAssetFactory.getRootResource();
    return classpathAssetFactory.createAsset(classpathRoot).toClientURL();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top