Wicket:如何使用<a href="..." target="_blank">

时间:2017-12-15 15:35:51

标签: java wicket

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) ?

2 个答案:

答案 0 :(得分:1)

我通过使用这个简单的标记创建Wickets Panel的扩展来解决问题:

<!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>

由于<a wicket:id="link"> Wicket没有为链接创建Javascript。

对应的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");
            }
        });
    }
}

在创建表列的代码中:

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"));
    }
});

答案 1 :(得分:0)

Java:

ExternalLink("link", new PropertyModel<>(model, "url"))

html:

<td><a target="_blank" wicket:id="link">Open</a></td>