Class StandardLinkBuilder

Object
org.thymeleaf.linkbuilder.AbstractLinkBuilder
org.thymeleaf.linkbuilder.StandardLinkBuilder
All Implemented Interfaces:
ILinkBuilder

public class StandardLinkBuilder extends AbstractLinkBuilder

Standard implementation of ILinkBuilder.

This class will build link URLs using (by default) the Java Servlet API when the specified URLs are context-relative, given the need to obtain the context path and add it to the URL. Also, when an IWebContext implementation is used as context, URLs will be passed to the standard HttpSerlvetResponse.encodeURL(...) method before returning.

Note however that the Servlet-API specific part of this behaviour is configurable and confined to a set of protected methods that can be overwritten by subclasses that want to offer a link building behaviour very similar to the standard one, but without any dependencies on the Servlet API (e.g. extracting URL context paths from a framework artifact other than HttpServletRequest).

This implementation will only return null at buildLink(IExpressionContext, String, Map) if the specified base argument is null.

Since:
3.0.0
Author:
Daniel Fernández
  • Constructor Details

    • StandardLinkBuilder

      public StandardLinkBuilder()
  • Method Details

    • buildLink

      public final String buildLink(IExpressionContext context, String base, Map<String,Object> parameters)
      Description copied from interface: ILinkBuilder

      Build a link, returning null if not possible.

      Parameters:
      context - the IExpressionContext object being used for template processing. Cannot be null.
      base - the base of the link URL to be built, i.e. its path. Can be null.
      parameters - the (optional) URL parameters.
      Returns:
      the built URL.
    • computeContextPath

      protected String computeContextPath(IExpressionContext context, String base, Map<String,Object> parameters)

      Compute the context path to be applied to URLs that have been determined to be context-relative (and therefore need a context path to be inserted at their beginning).

      By default, this method will obtain the context path from HttpServletRequest.getContextPath(), throwing an exception if context is not an instance of IWebContext given context-relative URLs are (by default) only allowed in web contexts.

      This method can be overridden by any subclasses that want to change this behaviour (e.g. in order to avoid using the Servlet API for resolving context path or to allow context-relative URLs in non-web contexts).

      Parameters:
      context - the execution context.
      base - the URL base specified.
      parameters - the URL parameters specified.
      Returns:
      the context path.
    • processLink

      protected String processLink(IExpressionContext context, String link)

      Process an already-built URL just before returning it.

      By default, this method will apply the HttpServletResponse.encodeURL(url) mechanism, as standard when using the Java Servlet API. Note however that this will only be applied if context is an implementation of IWebContext (i.e. the Servlet API will only be applied in web environments).

      This method can be overridden by any subclasses that want to change this behaviour (e.g. in order to avoid using the Servlet API).

      Parameters:
      context - the execution context.
      link - the already-built URL.
      Returns:
      the processed URL, ready to be used.