Java Vaadin 在新选项卡中打开链接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24039476/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 09:51:03  来源:igfitidea点击:

Vaadin open link in new tab

javavaadin

提问by user3702643

I have the following piece of code that I wrote using Vaadin. The code opens the page www.google.comwhen the user clicks the button.

我有以下使用 Vaadin 编写的代码。www.google.com当用户单击按钮时,代码会打开页面。

My question is is there any way for me to specify that the page is to be opened in a new tab?

我的问题是有什么方法可以让我指定要在新选项卡中打开该页面吗?

Thanks.

谢谢。

button.addClickListener(new Button.ClickListener() {
    public void buttonClick(ClickEvent event) {
        getUI().getPage().setLocation("http://www.google.com");
    }
});

采纳答案by VH-NZZ

getUI().getPage().open("http://www.google.com", "_blank");

The _blankwindow name is important here. Beware that you may also have browsers that willmight open the resource in a new window instead.

_blank窗口的名字是很重要的位置。要注意的是,你还可以有一个浏览器可以在新窗口中打开资源来代替。

There is also another signature to the open()method, i.e.

该方法还有另一个签名open(),即

open(String url, String windowName, boolean tryToOpenAsPopup) 

that may fit the bill. HTH.

这可能符合要求。哈。

References: Page (Vaadin 7.2.1 API).

参考资料:页面(Vaadin 7.2.1 API)

回答by sreg

Try the following code:

试试下面的代码:

BrowserWindowOpener opener = new BrowserWindowOpener(new ExternalResource(url));
opener.setFeatures("");
opener.extend(button);

回答by vitfo

It depends on what you want to achieve.

这取决于您想要实现的目标。

Solution 1

解决方案1

If you want your button to open a new tab, the BrowserWindowOpener might be the right solution.

如果您希望您的按钮打开一个新选项卡,BrowserWindowOpener 可能是正确的解决方案。

Button viewBtn = new Button("Click me");
BrowserWindowOpener opener = new BrowserWindowOpener(new ExternalResource("http://www.example.com"));
opener.setWindowName("_blank");
opener.extend(viewBtn);

Solution 2

解决方案2

Your button should open in a new tab only when Ctrl (Alt, Shift, ...) key on the keyboard is hold. If not, open in existing tab. In this case you can try to open a new tab using Page#open() method. Be aware that browsers will probably try to block your action and will warn user that they have blocked a pop-up window (even though it is not a pop-up but a new tab).

仅当按住键盘上的 Ctrl(Alt、Shift、...)键时,您的按钮才应在新选项卡中打开。如果没有,请在现有选项卡中打开。在这种情况下,您可以尝试使用 Page#open() 方法打开一个新选项卡。请注意,浏览器可能会尝试阻止您的操作,并会警告用户他们阻止了一个弹出窗口(即使它不是一个弹出窗口而是一个新选项卡)。

Button viewBtn = new Button("Click me", VaadinIcons.EYE);
viewBtn.addClickListener(ev -> {
  if (ev.isCtrlKey()) {
      Page.getCurrent().open("http://www.example.com", "_blank", false);
  } else {
      Page.getCurrent().setLocation("http://www.example.com");
  }
});

Solution 3

解决方案3

If you want the common behavior when the left click opens in existing tab and the middle mouse button click in a new tab, use the link instead of the button. In this case browsers probably let you open a new tab.

如果您想要在现有选项卡中单击左键并在新选项卡中单击鼠标中键时的常见行为,请使用链接而不是按钮。在这种情况下,浏览器可能会让您打开一个新选项卡。

Link link = new Link(null, new ExternalResource(UriUtil.createAdUri(ad)));
link.setIcon(VaadinIcons.EYE);

回答by Martin Vysny

Using Button, BrowserWindowOpenerand getUI().getPage().open("http://www.google.com", "_blank");is discouraged since that is usually blocked by popup blockers.

不鼓励使用Button,BrowserWindowOpenergetUI().getPage().open("http://www.google.com", "_blank");,因为它通常会被弹出窗口阻止程序阻止。

Instead go with the Linkcomponent:

而是使用Link组件:

final Link link = new Link("Google", new ExternalResource("http://www.google.com"));
link.setTargetName("_blank");

See more in the Vaadin Link Documentation

Vaadin 链接文档中查看更多信息