A comprehensive list of Apache Wicket's components.
With Label
components you can display dynamic
text in your pages and components.
add(new Label<String>("text", "Hello, World!"));
Like all Wicket components, the label needs a counterpart in the markup with the same wicket:id
:
<span wicket:id="text">gets replaced</span>
Using models you can display fields from your entities in your pages:
Person person = ...;
add(new Label<String>("text",
new PropertyModel<String>(person, "name")));
HTML notoriously strips newlines from your text and renders everything
like it is just one line of text. With MultiLineLabel
you
can render text like it is supposed to look with paragraphs and
line-breaks.
add(new Label("txt", "Hello,\nWorld!")); add(new MultiLineLabel("multi", "Hello,\nWorld!"));
Will render the following markup:
<p>Hello, World!</p> <p>Hello,<br />World!</p>
And that will result in text like:
Hello, World! Hello, World!
Notice that the first label is displayed on a single line, while the
MultiLineLabel
correctly renders the text across multiple
lines.
The associated markup tag can be anything: a <span>
, <a>
nchor,
<p>
aragraph, or even if you don't want a surrounding tag in the final markup
a <wicket:container>
. So for example:
<span wicket:id="t1"></span>
<p wicket:id="t2"></p>
<wicket:container wicket:id="t3"></wicket:container>
Will render as:
<span>Hello, World!</span>
<p>Hello, World!</p>
Hello, World!
You can also use label.setRenderBodyOnly(true)
to instruct Wicket to
just render the body.
By default, Wicket escapes all rendered text, preventing JavaScript injection attacks:
add(new Label("bad", "<a onclick=\"alert('Booh')\">Click me</a>"));
Will render safely as the following markup:
<a onclick=\"alert('Booh')\">Click me</a>
Which displays in the browser as:
<a onclick=\"alert('Booh')\">Click me</a>