Skip to content

Wicket: how to create ExternalLink with

Within a table cell, I have to create a Wicket ExternalLink that shows the response in a new browser tab. I’ve used this code (Wicket 7.9):

columns.add(new AbstractColumn<WebService, String>(Model.of(" "))
{
    @Override
    public void populateItem(Item<ICellPopulator<WebService>> cellItem, String componentId,
            IModel<WebService> serviceModel)
    {
        WebService ows = serviceModel.getObject();
        String url = "http://www.geodata.com/map?service=WMS&request=GetCapabilities";
        ExternalLink capabilitiesLink =
                new ExternalLink(componentId, Model.of(url), Model.of("Caps"))
                {
                    @Override
                    protected void onComponentTag(ComponentTag tag)
                    {
                        super.onComponentTag(tag);
                        tag.put("target", "_blank");
                    }
                };
        cellItem.add(capabilitiesLink);
    }
});

But Wicket renders this:
<div onclick="window.location.href='http://www.geodata.com/maps?service=WFS&amp;request=GetCapabilities';return false;" target="_blank">Caps</div>

This doesn’t open a new tab.

The tables HTML markup is generated by Wicket, so I can’t insert
<td><a wicket:id="link" href="..."></a></td> in the corresponding markup.

How can I force Wicket to render <a href=" ... " target="_blank">Caps</a> ?

Alternatively, how can I create a new browser tab when I can’t avoid using onClick (Javascript) ?

Answer

I solved the problem by creating an extension of Wickets Panel with this simple markup:

<!DOCTYPE html>
<html xmlns:wicket="http://wicket.apache.org">
<head>
    <meta charset="UTF-8">
</head>
<body>
<wicket:panel>
    <a wicket:id="link"></a>
</wicket:panel>
</body>
</html>

Because of <a wicket:id="link"> Wicket doesn’t create Javascript for the link.

Corresponding Java:

public class LinkTargetBlankPanel extends Panel
{
    private static final long serialVersionUID = -6527814402202345257L;

    public LinkTargetBlankPanel(String id, String url, String label)
    {
        super(id);
        add(new ExternalLink("link", url, label)
        {
            private static final long serialVersionUID = -8010560272317354356L;

            @Override
            protected void onComponentTag(ComponentTag tag)
            {
                super.onComponentTag(tag);
                tag.put("target", "_blank");
            }
        });
    }
}

And in the code that creates the table column:

columns.add(new AbstractColumn<WebService, String>(Model.of(" "))
{
    @Override
    public void populateItem(Item<ICellPopulator<WebService>> cellItem, String componentId,
            IModel<WebService> serviceModel)
    {
        String url = "http://www.geodata.com/map?service=WMS&request=GetCapabilities";
        cellItem.add(new LinkTargetBlankPanel("link", url, "GetCapabilities"));
    }
});