Class ThymeleafMixin
Compose into a host resource via
@Rest(mixins=ThymeleafMixin.class); the host then:
- Gains a default mount at
/thymeleaf/*that serves raw.htmltemplates from the importer's classpath by asking the configuredTemplateEngineto render them with the current request's locale and attributes available to the template. - Routes the host's own
@RestOp-method return values of typeThymeleafViewthrough the Thymeleaf engine automatically.ThymeleafMixindeclares@Rest(mergeResponseProcessorsIntoHost=true), so a plain@Rest(mixins=ThymeleafMixin.class)folds itsThymeleafViewRendererinto the host's own response-processor chain — nomergeIntoHostor explicitresponseProcessors=on the host needed.
Composition example (microservice):
Configurable mount path:
The default mount /thymeleaf/* can be overridden via the SVL variable
${juneau.thymeleaf.path:thymeleaf} — set via system property
(-Djuneau.thymeleaf.path=views), environment variable
(JUNEAU_THYMELEAF_PATH=views), or Config key
(juneau.thymeleaf.path = views) to change the runtime mount without subclassing.
Resolution happens once at RestContext construction time (SVL resolution in @RestOp(path)).
Mixin-only deployment:
This resource is designed for composition via @Rest(mixins=...). The mount path is
pinned at the op level by
@RestGet(path="/{juneau.thymeleaf.path:thymeleaf}/*") on render(java.lang.String, org.apache.juneau.rest.server.RestRequest, org.apache.juneau.rest.server.RestResponse);
a class-level @Rest(paths=...) declaration would be silently ignored under the mixin
pattern (see @Rest(paths) Javadoc).
Choosing a TemplateEngine:
The juneau-rest-server-view-thymeleaf module ships only the Thymeleaf core API in
provided scope. No engine bean is bundled. The bridge resolves a
TemplateEngine at first use from three sources, in order:
- Spring Boot autoconfig:
spring-boot-starter-thymeleafcontributes an autoconfiguredSpringTemplateEngine(which extendsTemplateEngine); the bridge picks it up viaBeanStore.getBean(TemplateEngine.class). No further wiring needed. - User-supplied bean: register a
@Bean TemplateEngine(Spring) orBasicBeanStore.put(TemplateEngine.class, engine)(microservice). Allows full control of resolvers, dialects, and template modes. - Bridge default: when nothing is registered, the bridge constructs a default
TemplateEnginewith a singleClassLoaderTemplateResolverconfigured withprefix=basePath,suffix=".html",templateMode=HTML, andcacheable=cacheTemplates(defaulttrue— production-safe; setcacheTemplates(false)on the builder for hot-reload during development).
When no Thymeleaf engine is on the classpath, the renderer surfaces
ThymeleafViewRenderer.NO_ENGINE_DIAGNOSTIC naming the missing dependency.
Multiple base paths:
A host with /templates/ and /admin/templates/ template trees registers two
ThymeleafMixin beans, each mounted via a subclass declaring its own op-level
@RestGet(path=...) override and its own basePath. See the
ThymeleafMixin_MockRest_Test test in juneau-integration-tests for the canonical
pattern.
OpenAPI surface:
The greedy /* handler is not API-meaningful and is excluded from generated
Swagger / OpenAPI specs via @OpSwagger(ignore=true).
See Also:
- Since:
- 10.0.0
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringDefault base path applied when noThymeleafMixin.Builder.basePath(String)call has been made.static final booleanDefault template-cache flag — production-safe; opt out per-builder for dev.static final org.thymeleaf.templatemode.TemplateModeDefault template mode for the bridge's fallback engine. -
Constructor Summary
ConstructorsModifierConstructorDescriptionNo-arg constructor — usesDEFAULT_BASE_PATHas the base path,DEFAULT_CACHE_TEMPLATESfor the cache flag, andDEFAULT_TEMPLATE_MODEfor the template mode.protectedThymeleafMixin(ThymeleafMixin.Builder builder) Builder constructor. -
Method Summary
Modifier and TypeMethodDescriptionstatic ThymeleafMixin.Buildercreate()Creates a new builder.Returns the base path under which template resources are resolved.org.thymeleaf.templatemode.TemplateModeReturns the template mode used by the bridge's default engine.booleanReturns whether the bridge's default engine caches resolved templates.voidrender(String path, RestRequest req, RestResponse res) [GET /thymeleaf/*] — render a raw template through the Thymeleaf engine.org.thymeleaf.TemplateEngineResolves the activeTemplateEnginevia the sharedThymeleafDispatcherworker.
-
Field Details
-
DEFAULT_BASE_PATH
Default base path applied when noThymeleafMixin.Builder.basePath(String)call has been made.- See Also:
-
DEFAULT_CACHE_TEMPLATES
Default template-cache flag — production-safe; opt out per-builder for dev.- See Also:
-
DEFAULT_TEMPLATE_MODE
Default template mode for the bridge's fallback engine.
-
-
Constructor Details
-
ThymeleafMixin
public ThymeleafMixin()No-arg constructor — usesDEFAULT_BASE_PATHas the base path,DEFAULT_CACHE_TEMPLATESfor the cache flag, andDEFAULT_TEMPLATE_MODEfor the template mode.The mixin walk falls back to this constructor when the
BeanStoredoes not have a registeredThymeleafMixinbean. -
ThymeleafMixin
Builder constructor.- Parameters:
builder- The builder. Must not benull.
-
-
Method Details
-
create
Creates a new builder.- Returns:
- A new builder.
-
getBasePath
Returns the base path under which template resources are resolved.- Returns:
- The base path. Never
null.
-
isCacheTemplates
Returns whether the bridge's default engine caches resolved templates.- Returns:
- The cache flag. Defaults to
DEFAULT_CACHE_TEMPLATES.
-
getTemplateMode
Returns the template mode used by the bridge's default engine.- Returns:
- The template mode. Defaults to
DEFAULT_TEMPLATE_MODE.
-
resolveTemplateEngine
Resolves the activeTemplateEnginevia the sharedThymeleafDispatcherworker.- Parameters:
req- The current REST request.- Returns:
- The active template engine. Never
null.
-
render
public void render(@Path("/*") String path, RestRequest req, RestResponse res) throws IOException, BasicHttpException [GET /thymeleaf/*] — render a raw template through the Thymeleaf engine.The
@Path("/*") String pathcaptures the multi-segment trailing remainder (e.g. a request for/thymeleaf/aboutmatches the mount withpath = "about"). Delegates to the sharedThymeleafDispatcherworker (.html-suffix handling, path-traversal hardening, engine resolution, and render).Missing template surfaces as the engine's own
TemplateInputException(HTTP 500 from the renderer's catch-all); missing engine surfaces asThymeleafViewRenderer.NO_ENGINE_DIAGNOSTIC.- Parameters:
path- The trailing path segment after/thymeleaf/(the template name relative to the configuredbase path; trailing.htmlstripped).req- The current REST request.res- The current REST response.- Throws:
IOException- If the underlying response writer fails.BasicHttpException- On boundary violation (403), missing engine (500), or render failure (500).
-