


Since Wicket uses plain HTML markup files as templates, we can place an anchor to an external page directly inside the markup file. When we need to dynamically generate external anchors, we can use link component _org.apache.wicket.markup.html.link.ExternalLink_. In order to build an external link we must specify the value of the href attribute using a model or a plain string. In the next snippet, given an instance of Person, we generate a Google search query for its full name:

Html:

[source,html]
----
<a wicket:id="externalSite">Search me on Google!</a>
----

Java code:

[source,java]
----
Person person = new Person("John", "Smith"); 
String fullName = person.getFullName();
//Space characters must be replaced by character '+'
String googleQuery = "http://www.google.com/search?q=" + fullName.replace(" ", "+");
add(new ExternalLink("externalSite", googleQuery));
----

Generated anchor:

[source,html]
----
<a href="http://www.google.com/search?q=John+Smith">Search me on Google!</a>
----

If we need to specify a dynamic value for the text inside the anchor, we can pass it as an additional constructor parameter:

Html:

[source,html]
----
<a wicket:id="externalSite">Label goes here...</a>
----

Java code:

[source,java]
----
Person person = new Person("John", "Smith"); 
String fullName = person.getFullName();
String googleQuery = "http://www.google.com/search?q=" + fullName.replace(" ", "+");
String linkLabel = "Search '" + fullName + "' on Google.";

add(new ExternalLink("externalSite", googleQuery, linkLabel));
----

Generated anchor:

[source,html]
----
<a href="http://www.google.com/search?q=John+Smith">Search 'John Smith' on Google.</a>
----

