Friday, June 1, 2012

Working with CSS & JS Files in Wicket

In Wicket, the preferred choice of serving CSS/JS files is as a resource from the JAR instead of from a physical file inside the web folder. The API provides an easy way to get it done that also comes with a lot of flexibility & modularity.

Basically you can call from anywhere in your Java code:
  • add(CSSPackageResource.getHeaderContribution(NiceButton.class, "button.css"));
And in the case of JavaScript:
  • add(JavascriptPackageResource.getHeaderContribution(SomePanel.class, "panel.js"));
And the links to the CSS & JS file will automatically be added to the header of the page.

This also means that at the Page class level, you can choose to either use the CSS file provided by the component, or override and add your own instead.

Also, there is an option to provide a location instead (the more traditional way for web-apps) with the "getHeaderContribution" method.

Reference: Wicket WIKI

No comments:

Post a Comment