blob: d4a11243108ab63f5fb189bcf068f47edad63f6a [file] [log] [blame]
Wicket comes with interface _org.apache.wicket.markup.html.IHeaderContributor_ which allows components and behaviors (which will be introduced later in <<_enriching_components_with_behaviors,paragraph 18.1>>) to contribute to the header section of their page. The only method defined in this interface is _renderHead(IHeaderResponse response)_ where _IHeaderResponse_ is an interface which defines method _render(HeaderItem item)_ to write static resources or free-form text into the header section of the page.
Header entries are instances of abstract class _org.apache.wicket.markup.head.HeaderItem_. Wicket provides a set of built-in implementations of this class suited for the most common types of resources. With the exception of _PriorityHeaderItem_, every implementation of _HeaderItem_ is an abstract factory class:
* *CssHeaderItem:* represents a CSS resource. Factory methods provided by this class are _forReference_ which takes in input a resource reference, _forUrl_ which creates an CSS item from a given URL and _forCSS_ which takes in input an arbitrary CSS string and an optional id value to identify the resource.
* *JavaScriptHeaderItem:* represents a JavaScript resource. Just like _CssHeaderItem_ it provides factory methods _forReference_ and _forUrl_ along with method _forScript_ which takes in input an arbitrary string representing the script and an optional id value to identify the resource. The returned type _JavaScriptReferenceHeaderItem_ exposes some interesting setting methods like _setDefer_ and _setAsync_ which can be used to set the corresponding attributes for https://www.w3schools.com/tags/tag_script.asp[script tag].
* *OnDomReadyHeaderItem:* it adds JavaScript code that will be executed after the DOM has been built, but before external files (such as picture, CSS, etc...) have been loaded. The class provides a factory method _forScript_ which takes in input an arbitrary string representing the script to execute.
* *OnEventHeaderItem:* the JavaScript code added with this class is executed when a specific JavaScript event is triggered on a given DOM element. The factory method is _forScript(String target, String event, CharSequence javaScript)_, where target is the id of a DOM element (or the element itself), event is the event that must trigger our code and javaScript is the code to execute.
* *OnLoadHeaderItem:* the JavaScript code added with this class is executed after the whole page is loaded, external files included. The factory method is _forScript(CharSequence javaScript)_.
* *PriorityHeaderItem:* it wraps another header item and ensures that it will have the priority over the other items during rendering phase.
* *StringHeaderItem:* with this class we can add an arbitrary text to the header section. Factory method is _forString(CharSequence string)_.
* *MetaDataHeaderItem:* Wicket provides this class to handle meta information such as <meta> tags or http://en.wikipedia.org/wiki/Canonical_link_element[canonical link element]
* *HtmlImportHeaderItem:* provides a HTML5 functionality to include other wicket pages (other html files) into the current generated. Factory methods provided by this class are _forImportLinkTag_ which takes the page class or the url of the page / html to be included.
In the following example our custom component loads a CSS file as a package resource (placed in the same package) and it adds it to header section.
[source,java]
----
public class MyComponent extends Component{
@Override
public void renderHead(IHeaderResponse response) {
PackageResourceReference cssFile =
new PackageResourceReference(this.getClass(), "style.css");
CssHeaderItem cssItem = CssHeaderItem.forReference(cssFile);
response.render(cssItem);
}
}
----