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 standardHttpSerlvetResponse.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 thanHttpServletRequest
).This implementation will only return
null
atbuildLink(IExpressionContext, String, Map)
if the specifiedbase
argument isnull
.- Since:
- 3.0.0
- Author:
- Daniel Fernández
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static class
StandardLinkBuilder.LinkType
-
Constructor Summary
Constructors Constructor Description StandardLinkBuilder()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description String
buildLink(IExpressionContext context, String base, Map<String,Object> parameters)
Build a link, returningnull
if not possible.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).protected String
processLink(IExpressionContext context, String link)
Process an already-built URL just before returning it.-
Methods inherited from class org.thymeleaf.linkbuilder.AbstractLinkBuilder
getName, getOrder, setName, setOrder
-
-
-
-
Method Detail
-
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
- theIExpressionContext
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 ifcontext
is not an instance ofIWebContext
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 ifcontext
is an implementation ofIWebContext
(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.
-
-