In Wicket 1.5, they're experimenting with different IHeaderRenderStrategy
implementations, such as ChildFirstHeaderRenderStrategy (experimental) or ParentFirstHeaderRenderStrategy (default). It seems that reordering header contributions was a problem in 1.4, so they're adressing it.
However, for Wicket 1.4, there is some kind of solution. The idea is to reorder the list of behaviors (header contributions are just yet another IBehavior). Each component has a protected getBehaviors()
method which can be overwritten. The trick is to find the behavior which corresponds to your special JS file which should be included first. I did this by creating my own instance of an HeaderContribution object, so I could perform an instance comparison and then use an ArrayList to move the HeaderContribution object from in the middle of the behavior list to the top of the behavior list:
public abstract class BasePage extends WebPage {
public BasePage() {
add(HeaderContributor.forJavaScript("foo-base.js"));
}
}
The HomePage adds the additional script, which should be rendered first. The HomePage also overwrites the geBehaviors() method:
public class HomePage extends BasePage {
private static final long serialVersionUID = 1L;
private final HeaderContributor contrib = new HeaderContributor(
new IHeaderContributor() {
public void renderHead(IHeaderResponse response) {
response.renderJavascriptReference("foo-first.js");
}
});
public HomePage(final PageParameters parameters) {
add(new Label("message", "If you see this message ..."));
add(contrib);
}
@Override
protected List getBehaviors(Class type) {
List behaviors = super.getBehaviors(type);
ArrayList sortedBehaviors = new ArrayList(behaviors);
boolean moveToTop = true;
if (moveToTop) {
if (!sortedBehaviors.remove(contrib)) {
throw new IllegalStateException();
}
sortedBehaviors.add(0, contrib);
}
return sortedBehaviors;
}
}
Now, the end result will be that the script added by HomePage is rendered first, and the script added by the BasPage comes afterwards.
<html xmlns:wicket="http://wicket.apache.org/... >
<head>
<title>Wicket Quickstart Archetype Homepage</title>
<script type="text/javascript" src="foo-first.js"></script>
<script type="text/javascript" src="foo-base.js"></script>
</head>
tag. I thought this is the proposed position by google anyway?
– bert Jan 05 '11 at 18:23