Class Page
- java.lang.Object
-
- com.vaadin.flow.component.page.Page
-
- All Implemented Interfaces:
Serializable
public class Page extends Object implements Serializable
Represents the web page open in the browser, containing the UI it is connected to.- Since:
- 1.0
- Author:
- Vaadin Ltd
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfacePage.ExecutionCancelerDeprecated.superseded byPendingJavaScriptResultstatic interfacePage.ExtendedClientDetailsReceiverCallback for receiving extended client-side details.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description RegistrationaddBrowserWindowResizeListener(BrowserWindowResizeListener resizeListener)Adds a newBrowserWindowResizeListenerto this UI.voidaddDynamicImport(String expression)Adds a dynamic import using a JavaScriptexpressionwhich is supposed to return a JavaScriptPromise.voidaddJavaScript(String url)Adds the given JavaScript to the page and ensures that it is loaded successfully.voidaddJavaScript(String url, LoadMode loadMode)Adds the given JavaScript to the page and ensures that it is loaded successfully.voidaddJsModule(String url)Adds the given external JavaScript module to the page and ensures that it is loaded successfully.voidaddStyleSheet(String url)Adds the given style sheet to the page and ensures that it is loaded successfully.voidaddStyleSheet(String url, LoadMode loadMode)Adds the given style sheet to the page and ensures that it is loaded successfully.Page.ExecutionCancelerexecuteJavaScript(String expression, Serializable... parameters)Deprecated.UseexecuteJs(String,Serializable...)instead since it also allows getting return value back.PendingJavaScriptResultexecuteJs(String expression, Serializable... parameters)Asynchronously runs the given JavaScript expression in the browser.voidfetchCurrentURL(SerializableConsumer<URL> callback)Retrieves the current url from the browser.HistorygetHistory()Gets a representation ofwindow.historyfor this page.voidopen(String url)Opens the given url in a new tab.voidopen(String url, String windowName)Opens the given URL in a window with the given name.voidreload()Reloads the page in the browser.voidretrieveExtendedClientDetails(Page.ExtendedClientDetailsReceiver receiver)Obtain extended client side details, such as time screen and time zone information, via callback.voidsetLocation(String uri)Navigates this page to the given URI.voidsetLocation(URI uri)Navigates this page to the given URI.voidsetTitle(String title)Sets the page title.
-
-
-
Constructor Detail
-
Page
public Page(UI ui)
Creates a page instance for the given UI.- Parameters:
ui- the UI that this page instance is connected to
-
-
Method Detail
-
setTitle
public void setTitle(String title)
Sets the page title. The title is displayed by the browser e.g. as the title of the browser window or tab.To clear the page title, use an empty string.
- Parameters:
title- the page title to set, notnull
-
addStyleSheet
public void addStyleSheet(String url)
Adds the given style sheet to the page and ensures that it is loaded successfully.Relative URLs are interpreted as relative to the static web resources directory. So if the
urlvalue is"some.js"andsrc/main/webappis used as a location for static web resources (which is the default location) then the file system path for the resource should besrc/main/webapp/some.js.You can prefix the URL with
context://to make it relative to the context path or use an absolute URL to refer to files outside the frontend directory.For component related style sheet dependencies, you should use the
@StyleSheetannotation.Is is guaranteed that style sheet will be loaded before the first page load. For more options, refer to
addStyleSheet(String, LoadMode)- Parameters:
url- the URL to load the style sheet from, notnull
-
addStyleSheet
public void addStyleSheet(String url, LoadMode loadMode)
Adds the given style sheet to the page and ensures that it is loaded successfully.Relative URLs are interpreted as relative to the static web resources directory. So if the
urlvalue is"some.js"andsrc/main/webappis used as a location for static web resources (which is the default location) then the file system path for the resource should besrc/main/webapp/some.js.You can prefix the URL with
context://to make it relative to the context path or use an absolute URL to refer to files outside the frontend directory.For component related style sheet dependencies, you should use the
@StyleSheetannotation.- Parameters:
url- the URL to load the style sheet from, notnullloadMode- determines dependency load mode, refer toLoadModefor details
-
addJavaScript
public void addJavaScript(String url)
Adds the given JavaScript to the page and ensures that it is loaded successfully.Relative URLs are interpreted as relative to the static web resources directory. So if the
urlvalue is"some.js"andsrc/main/webappis used as a location for static web resources (which is the default location) then the file system path for the resource should besrc/main/webapp/some.js.You can prefix the URL with
context://to make it relative to the context path or use an absolute URL to refer to files outside the frontend directory.For component related JavaScript dependencies, you should use the
@JavaScriptannotation.Is is guaranteed that script will be loaded before the first page load. For more options, refer to
addJavaScript(String, LoadMode)- Parameters:
url- the URL to load the JavaScript from, notnull
-
addJavaScript
public void addJavaScript(String url, LoadMode loadMode)
Adds the given JavaScript to the page and ensures that it is loaded successfully.Relative URLs are interpreted as relative to the static web resources directory. So if the
urlvalue is"some.js"andsrc/main/webappis used as a location for static web resources (which is the default location) then the file system path for the resource should besrc/main/webapp/some.js.You can prefix the URL with
context://to make it relative to the context path or use an absolute URL to refer to files outside the frontend directory.For component related JavaScript dependencies, you should use the
@JavaScriptannotation.- Parameters:
url- the URL to load the JavaScript from, notnullloadMode- determines dependency load mode, refer toLoadModefor details
-
addJsModule
public void addJsModule(String url)
Adds the given external JavaScript module to the page and ensures that it is loaded successfully.If the JavaScript modules do not need to be added dynamically, you should use the
@JsModuleannotation instead.- Parameters:
url- the URL to load the JavaScript module from, notnull
-
addDynamicImport
public void addDynamicImport(String expression)
Adds a dynamic import using a JavaScriptexpressionwhich is supposed to return a JavaScriptPromise.No change will be applied on the client side until resulting
Promiseof theexpressionis completed. It behaves like other dependencies (addJavaScript(String),addJsModule(String), etc.)- Parameters:
expression- the JavaScript expression which return a Promise
-
executeJavaScript
@Deprecated public Page.ExecutionCanceler executeJavaScript(String expression, Serializable... parameters)
Deprecated.UseexecuteJs(String,Serializable...)instead since it also allows getting return value back.Asynchronously runs the given JavaScript expression in the browser. The given parameters will be available to the expression as variables named$0,$1, and so on. Supported parameter types are:StringIntegerDoubleBooleanJsonValueElement(will be sent asnullif the server-side element instance is not attached when the invocation is sent to the client)
'prefix' + $0instead of'prefix$0'andvalue[$0]instead ofvalue.$0since JavaScript variables aren't evaluated inside strings or property names.- Parameters:
expression- the JavaScript expression to invokeparameters- parameters to pass to the expression- Returns:
- a callback for canceling the execution if not yet sent to browser
-
executeJs
public PendingJavaScriptResult executeJs(String expression, Serializable... parameters)
Asynchronously runs the given JavaScript expression in the browser.The returned
PendingJavaScriptResultcan be used to retrieve anyreturnvalue from the JavaScript expression. If no return value handler is registered, the return value will be ignored.The given parameters will be available to the expression as variables named
$0,$1, and so on. Supported parameter types are:StringIntegerDoubleBooleanJsonValueElement(will be sent asnullif the server-side element instance is not attached when the invocation is sent to the client)
'prefix' + $0instead of'prefix$0'andvalue[$0]instead ofvalue.$0since JavaScript variables aren't evaluated inside strings or property names.- Parameters:
expression- the JavaScript expression to invokeparameters- parameters to pass to the expression- Returns:
- a pending result that can be used to get a value returned from the expression
-
getHistory
public History getHistory()
Gets a representation ofwindow.historyfor this page.- Returns:
- the history representation
-
reload
public void reload()
Reloads the page in the browser.
-
addBrowserWindowResizeListener
public Registration addBrowserWindowResizeListener(BrowserWindowResizeListener resizeListener)
Adds a newBrowserWindowResizeListenerto this UI. The listener will be notified whenever the browser window within which this UI resides is resized.- Parameters:
resizeListener- the listener to add, notnull- Returns:
- a registration object for removing the listener
- See Also:
BrowserWindowResizeListener.browserWindowResized(BrowserWindowResizeEvent),Registration
-
open
public void open(String url)
Opens the given url in a new tab.- Parameters:
url- the URL to open.
-
open
public void open(String url, String windowName)
Opens the given URL in a window with the given name.The supplied
windowNameis used as the target name in a window.open call in the client. This means that special values such as "_blank", "_self", "_top", "_parent" have special meaning. An empty ornullwindow name is also a special case."", null and "_self" as
windowNameall causes the URL to be opened in the current window, replacing any old contents. For downloadable content you should avoid "_self" as "_self" causes the client to skip rendering of any other changes as it considers them irrelevant (the page will be replaced by the response from the URL). This can speed up the opening of a URL, but it might also put the client side into an inconsistent state if the window content is not completely replaced e.g., if the URL is downloaded instead of displayed in the browser."_blank" as
windowNamecauses the URL to always be opened in a new window or tab (depends on the browser and browser settings)."_top" and "_parent" as
windowNameworks as specified by the HTML standard.Any other
windowNamewill open the URL in a window with that name, either by opening a new window/tab in the browser or by replacing the contents of an existing window with that name.- Parameters:
url- the URL to open.windowName- the name of the window.
-
setLocation
public void setLocation(String uri)
Navigates this page to the given URI. The contents of this page in the browser is replaced with whatever is returned for the given URI.- Parameters:
uri- the URI to show
-
setLocation
public void setLocation(URI uri)
Navigates this page to the given URI. The contents of this page in the browser is replaced with whatever is returned for the given URI.- Parameters:
uri- the URI to show
-
retrieveExtendedClientDetails
public void retrieveExtendedClientDetails(Page.ExtendedClientDetailsReceiver receiver)
Obtain extended client side details, such as time screen and time zone information, via callback. If already obtained, the callback is called directly. Otherwise, a client-side roundtrip will be carried out.- Parameters:
receiver- the callback to which the details are provided
-
fetchCurrentURL
public void fetchCurrentURL(SerializableConsumer<URL> callback)
Retrieves the current url from the browser. The URL is fetched from the browser in another request asynchronously and passed to the callback. The URL is the full URL that the user sees in the browser (including hash #) and works even when client side routing is used or there is a reverse proxy between the client and the server.In case you need more control over the execution you can use
executeJs(String, Serializable...)by passingreturn window.location.href.NOTE: the URL is not escaped, use
URL.toURI()to escape it.- Parameters:
callback- to be notified when the url is resolved.
-
-