How to get browser address in vaadin 13?

I’m completely new to this framework. From my understanding previous versions had something like this Page.getCurrent().getWebBrowser().getAddress() , but I can’t find a way to do the same thing in Vaadin 13.

Hi,

I don’t know why you need the address and when you need it but you can get the address with event.getLocation() on some navigation events, see here https://vaadin.com/docs/v13/flow/routing/tutorial-routing-lifecycle.html

Jean-Christophe Gueriaud:
Hi,

I don’t know why you need the address and when you need it

Use case example: My Vaadin web server runs on Tomcat. On the same Tomcat, same port, I run a REST service. Want to access that REST service from my vaadin APP. Want to infer the REST service address from the current Vaadin App URL address. Makes sense ? Would you suggest different way ? (I would prefer avoiding configuration file)

Hi,

I’m not sure if it’s too late in the navigation livecycle but you can retrieve the location of the page in the AfterNavigation:


public class YourView extends Div implements AfterNavigationListener {
 ...

    @Override
    public void afterNavigation(AfterNavigationEvent event) {
        // event.getLocation() 
    }
}

I would probably try to use the parameters and the route name instead of the url but it depends of your architecture.

Jean-Christophe Gueriaud:
Hi,

I don’t know why you need the address and when you need it

Use case example: My Vaadin web server runs on Tomcat. On the same Tomcat, same port, I run a REST service. Want to access that REST service from my vaadin APP. Want to infer the REST service address from the current Vaadin App URL address. Makes sense ? Would you suggest different way ? (I would prefer avoiding configuration file)

browser javascript has a simple client side solution: document.location
So on java server side, I tried:

@Route("")
public class MainView extends VertLayout implements BeforeEnterObserver
{
    String URLlocation;
    @Override
    public void beforeEnter(BeforeEnterEvent event)
    {
        Page page = UI.getCurrent().getPage();
        PendingJavaScriptResult callBack = page.executeJs("console.log(document.location); return document.location;");
        callBack.then
        (
            JreJsonObject.class,
            result -> URLlocation = result.get("href").asString()
        );
    }	
}

This worked for me on Vaadin 17