这添加两个label的例子,如下所示。
其中,homepage.java
public class HomePage extends WebPage{
public HomePage() {
add(new Label("onComponentTag", "Test value"){
@Override
protected void onComponentTag(ComponentTag tag) {
super.onComponentTag(tag);
//Turn the h1 tag to a span
tag.setName("span");
//Add formatting style
tag.put("style", "font-weight:bold");
}
});
//onComponentTagBody example
add(new Label("onComponentTagBody", ""){
@Override
public void onComponentTagBody(MarkupStream markupStream,
ComponentTag tag) {
if(!isEnabled())
replaceComponentTagBody(markupStream, tag, "(the component is disabled)");
else
super.onComponentTagBody(markupStream, tag);
}
}.setEnabled(false));
}
}
html文件
<html lang="en" xmlns:wicket="http://wicket.apache.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
This label overrides onComponentTag and turns tag h1 to
span:
<h1 wicket:id="onComponentTag">Test value</h1>
<br />
This label overrides onComponentTagBody and displays a
custom value when it is disabled:
<span wicket:id="onComponentTagBody">Test value</span>
</body>
</html>
webapplication
public class WicketApplication extends WebApplication {
@Override
public Class<? extends Page> getHomePage() {
return HomePage.class;
}
}