Class Location

java.lang.Object
com.vaadin.flow.router.Location
All Implemented Interfaces:
Serializable

public class Location extends Object implements Serializable
Represents a relative URL made up of path segments and query parameters, but lacking e.g. the hostname that can also be present in URLs.

For related utility methods, see LocationUtil.

Since:
1.0
Author:
Vaadin Ltd
See Also:
  • Constructor Details

    • Location

      public Location(String location) throws InvalidLocationException
      Creates a new Location object for given location string.

      This string can contain relative path and query parameters, if needed. A possible fragment #fragment is also retained.

      A possible "/" prefix of the location is ignored and a null location is interpreted as ""

      Parameters:
      location - the relative location or null which is interpreted as ""]
      Throws:
      InvalidLocationException - If the given string cannot be used for the Location
    • Location

      public Location(String location, QueryParameters queryParameters) throws InvalidLocationException
      Creates a new Location object for given location string and query parameters.

      The location string can not contain query parameters. To pass query parameters, either specify them in QueryParameters in this constructor, or use Location(String)

      A possible "/" prefix of the location is ignored and a null location is interpreted as ""

      Parameters:
      location - the relative location or null which is interpreted as ""
      queryParameters - query parameters information, not null
      Throws:
      InvalidLocationException - If the given string cannot be used for the Location
    • Location

      public Location(List<String> segments)
      Creates a new location based on a list of path segments.
      Parameters:
      segments - a non-empty list of path segments, not null
    • Location

      public Location(List<String> segments, QueryParameters queryParameters)
      Creates a new location based on a list of path segments and query parameters.
      Parameters:
      segments - a non-empty list of path segments, not null and not empty
      queryParameters - query parameters information, not null
  • Method Details

    • getSegments

      public List<String> getSegments()
      Gets all the path segments of this location.
      Returns:
      a list of path segments
    • getQueryParameters

      public QueryParameters getQueryParameters()
      Gets the request parameters used for current location.
      Returns:
      the request parameters
    • getFirstSegment

      public String getFirstSegment()
      Gets the first segment of this path.
      Returns:
      the first path segment, not null
    • getSubLocation

      public Optional<Location> getSubLocation()
      Creates a new location without the first path segment. The result is empty if this location only consists of one segment.
      Returns:
      an optional new location, or an empty optional if this location has only one path segment
    • getPath

      public String getPath()
      Gets the path of this location as a string.
      Returns:
      the location string, not null
    • getPathWithQueryParameters

      public String getPathWithQueryParameters()
      Gets the path string with QueryParameters and including the possible fragment if one existed.
      Returns:
      path string with parameters
    • toggleTrailingSlash

      public Location toggleTrailingSlash()
      Removes or adds slash to the end of the location path. Creates new Location instance instead of modifying the old one.
      Returns:
      new Location instance with updated path