headタグの記述を修正しようと、wicketのユーザーガイドをまさぐっていたところ、
<wicket:header-items/>
と云うタグがあるっぽいことを知りましたので、
以下、使い方についてメモします。
ユーザーガイドの抜粋
15.9 Header contributors positioning
Starting from version 6.15.0 we can specify where header contributors must be rendered inside tag using the placeholder tag
<wicket:header-items/>
:
<head>
<meta charset="UTF-8"/>
<wicket:header-items/>
<script src="my-monkey-patch-of-wicket-ajax.js"></script>
</head>
With the code above all header contributions done by using IHeaderResponse in your Java code or the special
<wicket:head>
tag will be put between the<meta>
and<script>
elements, i.e. in the place of<wicket:header-items/>
.This way you can make sure that some header item is always before or after the header items managed by Wicket.
<wicket:header-items/>
can be used only in the page’s<head>
element and there could be at most one instance of it.
ユーザーガイドの抜粋(訳)
15.9 ヘッダー記述の挿入位置を決める
バージョン6.15.0から、
<head>
のタグの中にレンダリングしなければならない場合の、リプレイスホルダーとして、<wicket:header-items/>
が使用できるようになりました。
<head>
<meta charset="UTF-8"/>
<wicket:header-items/>
<script src="my-monkey-patch-of-wicket-ajax.js"></script>
</head>
javaコード内で、IHeaderResponseで追加したヘッダー要素、
<wicket:head>
内に記述したヘッダー要素のが全て、
<meta>
タグ、<script>
タグの間、すなわち、<wicket:header-items/>
の記述箇所に、挿入されます。<wicket:header-items/>
タグの上下のタグは、Wicketによって変更されることはなく、
あなたは常に上下のタグが変わらないことを確認できます。
<wicket:header-items/>
は<head>
タグ内に1つだけ定義できます。
metaタグ、scriptタグがなくても、以下の記述で動作する。
Documentにはmetaタグと、scriptタグの間に記述している例になっているますが、
以下、記述で動作しました。
<head>
<wicket:header-items/>
</head>
Panelクラスに記述したら、エラーになった。
初めはPanelクラスに記述していたのですが、
以下のエラーが出力され、一向に動きませんでした。
<wicket:head>
タグがあるので、子クラス側で必要になるユースケースがない?
<wicket:head>
タグを使用すればよいということだと理解しました。
org.apache.wicket.markup.MarkupException: Failed to handle: <wicket:header-items>. It might be that no resolver has been registered to handle this special tag. But it also could be that you declared wicket:id=wicket_header-items4 in your markup, but that you either did not add the component to your page at all, or that the hierarchy does not match. Container: [HeaderTagPanel [Component id = headerTagPanel]]
! at org.apache.wicket.markup.MarkupStream.throwMarkupException(MarkupStream.java:526) ~[xxx-0.0.1.jar:0.0.1]
! at org.apache.wicket.MarkupContainer.throwException(MarkupContainer.java:1591) ~[xxx-0.0.1.jar:0.0.1]
! at org.apache.wicket.MarkupContainer.renderNext(MarkupContainer.java:1537) ~[xxx-0.0.1.jar:0.0.1]
! at org.apache.wicket.MarkupContainer.renderAll(MarkupContainer.java:1759) ~[xxx-0.0.1.jar:0.0.1]
! at org.apache.wicket.MarkupContainer.renderComponentTagBody(MarkupContainer.java:1734) ~[xxx-0.0.1.jar:0.0.1]
! at org.apache.wicket.MarkupContainer.renderAssociatedMarkup(MarkupContainer.java:797) ~[xxx-0.0.1.jar:0.0.1]
! at org.apache.wicket.markup.html.panel.AssociatedMarkupSourcingStrategy.renderAssociatedMarkup(AssociatedMarkupSourcingStrategy.java:77) ~[xxx-0.0.1.jar:0.0.1]
実際に書いたコードの抜粋
- HTML
<head>
<meta charset="utf-8">
<title wicket:id="headerTitle">Example Page</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<wicket:header-items/>
</head>
- Page#renderHead(IHeaderResponse response)
@Override
public void renderHead(IHeaderResponse response) {
super.renderHead(response);
// Meta Descreption
response.render(MetaDataHeaderItem.forMetaTag("description", getString("content")));
// Meta AUTHOR
response.render(MetaDataHeaderItem.forMetaTag("author", getString("author")));
// Meta Favicon
response.render(MetaDataHeaderItem.forLinkTag("shortcut icon", "/static/images/favicon.ico"));
// Web Fonts
response.render(CssHeaderItem.forUrl("http://fonts.googleapis.com/css?family=Open+Sans:400italic,700italic,400,700,300&subset=latin,latin-ext"));
response.render(CssHeaderItem.forUrl("http://fonts.googleapis.com/css?family=Raleway:700,400,300"));
// Jquery
response.render(JavaScriptHeaderItem.forUrl("/static/plugins/jquery.min.js"));
response.render(JavaScriptHeaderItem.forUrl("/static/plugins/jquery.tile.min.js"));
}
@Override
protected void onInitialize() {
super.onInitialize();
// add headerTitle
add(new Label("headerTitle", getRBString("siteTitle")));
}
- 補足
<header>
内の<title>
タグを生成するできるクラスが、
StringHeaderItem
しかなさそうです。
title を動的に変更したかったため、
Label 化して、別途設定するようにしました。
以上です。
コメント