3.5.22. Opening External URLs

WebBrowserTools is a utility bean for opening external URLs. While the BrowserFrame component displays embedded web pages within the application, WebBrowserTools enables accessing external URLs in a user’s web browser tab.

WebBrowserTools is a functional interface that contains a single method: void showWebPage(String url, @Nullable Map<String, Object> params).

@Inject
private WebBrowserTools webBrowserTools;

@Subscribe("button")
public void onButtonClick(Button.ClickEvent event) {
    webBrowserTools.showWebPage("https://cuba-platform.com", ParamsMap.of("_target", "blank"));
}

The showWebPage() method may take optional parameters:

  • target - String value used as the target name in a window.open call in the client. This means that only the values "_blank", "_self", "_top", and "_parent" will be considered. If not specified, "_blank" is used.

  • width - Integer value specifying the width of the browser window in pixels.

  • height - Integer value specifying the height of the browser window in pixels.

  • border - String value specifying the border style of the window of the browser window. Possible values are "DEFAULT", "MINIMAL", "NONE".

WebBrowserTools is not a regular Spring bean, so you can only inject it into a screen controller or obtain via AppUI.getCurrent().getWebBrowserTools(). Don’t try to inject it to Spring beans or get using AppBeans.get().

For example, to open a URL directly from a menu item you should create a class implementing Runnable and, as far as the injection is not available here, use the AppUI.getCurrent() static method:

public class ExternalUrlMenuItemRunner implements Runnable {

    @Override
    public void run() {
        AppUI.getCurrent().getWebBrowserTools().showWebPage("http://www.cuba-platform.com", null);
    }
}

See more on AppUI class in the Generic UI Infrastructure section.