Customize Java Tapestry @IncludeStylesheet annotation
Using Tapestry 5, I'm looking at a (if possible) elegant way to c开发者_开发百科ustomize the @IncludeStylesheet
annotation in order to automatically add a version number to the style sheet file name.
For instance
@IncludeStylesheet("context:/css/decoration.css")
would generate automatically the CSS inclusion with a v
ersion number
<link href="/css/decoration.css?v=12345" rel="stylesheet" type="text/css">
Adding a new annotation, like @IncludeStylesheetVersion
was my initial idea.
RenderSupport
and DocumentLinker
(among others).
Would there be a simpler way to proceed?
You may not have to do that at all: Asset versioning is built into Tapestry. The default behaviour, according to the docs:
Tapestry creates a new URL for assets (whether context or classpath). The URL is of the form
/assets/application version number/folder/path
.
- application version number: Defined by symbol
tapestry.application-version
, the default value is a random hex number.
If you just need to make sure clients will see changed stylesheets after an update and you don't restart your app all the time, the default behavior (random hex number) should do just fine.
Edit based on comment: This is also available in 5.1:
- app/app-version (for assets obtained at or beneath the application package)
- classpath/app-version (for assets obtained from any otherwise
unmapped package)Where version is the Tapestry framework version, and app-version is the application version (which will be a random string if not explicitly configured).
Just check the URLs Tapestry generates for your CSS files, they should look like this: http://myserver.com/assets/ctx/942f15f778dca26c/css/styles.css
Overall, the effort you'd put into upgrading to 5.3 will probably be better spent than trying to create an annotation to mimic the built in functionality of current versions. I think you'll find the upgrade process pretty painless--particularly if you have good integration testing in place.
精彩评论