public class TemplateContext
extends java.lang.Object
Template context object is thread-bound and available during template rendering request processing.
| Constructor and Description |
|---|
TemplateContext(java.lang.String appName,
java.lang.String rootUrl,
java.lang.String restSubContext,
java.lang.String restPrefix,
boolean directTemplate,
AssetLookup assets,
ErrorRedirect errorRedirect,
javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response) |
| Modifier and Type | Method and Description |
|---|---|
java.lang.String |
getAppName() |
static TemplateContext |
getInstance() |
javax.servlet.http.HttpServletRequest |
getRequest()
Method may be used to access original request object (in edge cases).
|
javax.servlet.http.HttpServletResponse |
getResponse()
Raw response is required for redirection logic to avoid response processing loops
due to hk wrappers (if hk injection were used for response object injection it would always be a proxy).
|
java.lang.String |
getRestPrefix()
Context rest mapping path.
|
java.lang.String |
getRestSubContext()
Different rest prefix may be mapped to sub context (e.g.
|
java.lang.String |
getRootUrl() |
java.lang.String |
getUrl()
Each template render is redirected to rest resource so it's impossible to obtain original uri from request
object inside the resource.
|
boolean |
isDirectTemplate()
True means that one of registered view renderers recognize path as template file.
|
java.net.URL |
loadAsset(java.lang.String path)
Load asset from one of registered locations.
|
java.lang.String |
lookupTemplatePath(java.lang.String template)
Lookup relative template path either relative to resource class (if annotated with
Template or
in one of pre-configured classpath locations. |
boolean |
redirectError(java.lang.Throwable ex)
Perform redirection to error page (if registered) or handle SPA route (if 404 response and SPA support enabled).
|
void |
setAnnotationTemplate(java.lang.String template)
Used by
TemplateAnnotationFilter to set template file
declared in Template annotation on rest resource. |
void |
setManualErrorHandling(boolean manualErrors)
Disables GSP error pages support.
|
void |
setResourceClass(java.lang.Class base)
Set resource class to check template relative to class.
|
public TemplateContext(java.lang.String appName,
java.lang.String rootUrl,
java.lang.String restSubContext,
java.lang.String restPrefix,
boolean directTemplate,
AssetLookup assets,
ErrorRedirect errorRedirect,
javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response)
public static TemplateContext getInstance()
public java.lang.String getAppName()
public java.lang.String getRootUrl()
public java.lang.String getRestSubContext()
public java.lang.String getRestPrefix()
public boolean isDirectTemplate()
public java.lang.String getUrl()
public javax.servlet.http.HttpServletRequest getRequest()
for original request URI,
for root mapping urlpublic javax.servlet.http.HttpServletResponse getResponse()
Method may be used to handle response directly (in edge cases)
public void setResourceClass(java.lang.Class base)
TemplateAnnotationFilter.base - resource classpublic void setAnnotationTemplate(java.lang.String template)
TemplateAnnotationFilter to set template file
declared in Template annotation on rest resource.template - template file pathpublic void setManualErrorHandling(boolean manualErrors)
ManualErrorHandling annotation on resource
method or resource itself. Used by TemplateAnnotationFilter.
May be set manually, but it is not recommended - prefer annotations usage to clearly declare "exceptions" from global errors handling.
manualErrors - true to disable GSP errors handling, false to activate GSP error pagespublic java.lang.String lookupTemplatePath(@Nullable
java.lang.String template)
Template or
in one of pre-configured classpath locations. If passed template is null it will be
taken from Template annotation from resource class.
When provided template path is absolute - it is searched by direct location only.
template - template path or nulljava.lang.NullPointerException - if template path not setTemplateNotFoundException - if template not foundpublic java.net.URL loadAsset(java.lang.String path)
Method assumes to load absolute classpath location (through all custom class loaders, if registered). But, if direct lookup fails, it will perform relative resolution (search in all registered locations).
If custom class loaders used for assets declarations then template engines must be customized to resolve
templates through this method (otherwise they would not be able to find it in custom class loader).
For freemarker integration already provided and could be activated with
ServerPagesBundle.ViewsBuilder.enableFreemarkerCustomClassLoadersSupport().
path - absolute or relative pathpublic boolean redirectError(java.lang.Throwable ex)
When only resulted status code is known use WebApplicationException(code) as argument for redirection.
It is safe to call redirection multiple times: only first call will be actually handled (assuming next errors appear during error page rendering and can't be handled).
Method is not intended to be used directly, but could be in specific (maybe complex) edge cases.
ex - exception instance