This Confluence has been LDAP enabled, if you are an ASF Committer, please use your LDAP Credentials to login. Any problems file an INFRA jira ticket please.

Child pages
  • Adding Javascript or CSS using a Resource
Skip to end of metadata
Go to start of metadata

Wicket 1.3 and earlier

To add Javascript or CSS to your pages and have them compressed during the "deployment" cycle automatically by Wicket, start with the Javascript or CSS file somewhere in our package hierarchy that we can reference in our Page. For simplicity, we can copy it right next to the HTML file like so:

MyPage.java
MyPage.html
MyPage.js
MyPage.css

Then in our Page, we need to reference these items based on the path of our Java file, like so:

private static final CompressedResourceReference MYPAGE_JS = new CompressedResourceReference(MyPage.class, "MyPage.js");

private static final CompressedResourceReference MYPAGE_CSS = new CompressedResourceReference(MyPage.class, "MyPage.css");

This code gives us a ResourceReference that we can add to our page, most use cases to the HTML head element block. To do that in your page:

add(HeaderContributor.forJavaScript( MYPAGE_JS ));

add(HeaderContributor.forCss( MYPAGE_CSS ));

Wicket 1.4

In Wicket 1.4 HeaderContributor.forJavaScript() and HeaderContributor.forCss() are deprecated, so use the code below:

add(JavascriptPackageResource.getHeaderContribution(MYPAGE_JS));

add(CSSPackageResource.getHeaderContribution(MYPAGE_CSS));

Or

public class MyPage extends WebPage {
...
  @Override
  public void renderHead(IHeaderResponse response) {
    super.renderHead(response);
    response.renderJavascriptReference(new JavascriptResourceReference(MyPage.class, "script.js"));
  }
...

Also see the example for Wicket 1.5.

Wicket 1.5

In Wicket 1.5, CompressedResourceReference has been removed!

Also, the getHeaderContribution functionality has been removed. Instead, your component (Page, Panel, etc.) needs to override renderHead(IHeaderResponse):

public class MyPage extends WebPage implements IHeaderContributor {
...
  @Override
  public void renderHead(IHeaderResponse response) {
    response.renderJavaScriptReference("urlHere");
  }

Wicket 6.x

renderJavaScriptReference seems to be gone.

...
private static final JavaScriptResourceReference MYPAGE_JS = new JavaScriptResourceReference(MyPage.class, "MyPage.js");
...
@Override
public void renderHead(IHeaderResponse response) {
  response.render(JavaScriptReferenceHeaderItem.forReference(MYPAGE_JS));
}
...