Thymeleaf CDI Integration

An extension for Thymeleaf that allows referencing CDI @Named beans as top-level objects in expressions.

License

License

Categories

Categories

Leaf Data Databases
GroupId

GroupId

hu.inbuss
ArtifactId

ArtifactId

thymeleaf-cdi
Last Version

Last Version

0.0.9
Release Date

Release Date

Type

Type

jar
Description

Description

Thymeleaf CDI Integration
An extension for Thymeleaf that allows referencing CDI @Named beans as top-level objects in expressions.
Project URL

Project URL

https://github.com/inbuss/thymeleaf-cdi
Source Code Management

Source Code Management

http://github.com/inbuss/thymeleaf-cdi/tree/master

Download thymeleaf-cdi

How to add to project

<!-- https://jarcasting.com/artifacts/hu.inbuss/thymeleaf-cdi/ -->
<dependency>
    <groupId>hu.inbuss</groupId>
    <artifactId>thymeleaf-cdi</artifactId>
    <version>0.0.9</version>
</dependency>
// https://jarcasting.com/artifacts/hu.inbuss/thymeleaf-cdi/
implementation 'hu.inbuss:thymeleaf-cdi:0.0.9'
// https://jarcasting.com/artifacts/hu.inbuss/thymeleaf-cdi/
implementation ("hu.inbuss:thymeleaf-cdi:0.0.9")
'hu.inbuss:thymeleaf-cdi:jar:0.0.9'
<dependency org="hu.inbuss" name="thymeleaf-cdi" rev="0.0.9">
  <artifact name="thymeleaf-cdi" type="jar" />
</dependency>
@Grapes(
@Grab(group='hu.inbuss', module='thymeleaf-cdi', version='0.0.9')
)
libraryDependencies += "hu.inbuss" % "thymeleaf-cdi" % "0.0.9"
[hu.inbuss/thymeleaf-cdi "0.0.9"]

Dependencies

compile (1)

Group / Artifact Type Version
org.thymeleaf : thymeleaf jar 3.0.2.RELEASE

provided (2)

Group / Artifact Type Version
javax.servlet : servlet-api jar 2.5
javax.enterprise : cdi-api jar 1.2

Project Modules

There are no modules declared in this project.

thymeleaf-cdi

An extension for Thymeleaf that allows referencing CDI named beans as top-level objects in expressions. This works with the Standard Dialect and its OGNL-based expression language - Spring users would probably not be interested in CDI integration anyway.

Dependencies

This extension is a bridge between the CDI standard and Thymeleaf; your project should already contain both Thymeleaf and the CDI API.

The extension targets the CDI 1.2 standard. It may run on earlier versions as well, but this was not tested. An implementation of the standard must be provided by the environment or the application at runtime. (The extension was tested using Weld 2.3.5, but any compliant implementation should work.)

The Thymeleaf side is written against the Thymeleaf 3 API. Earlier versions cannot be reasonably supported due to the lack of lazy context variable evaluation.

Usage

  1. Include the library in your project. For example with Maven:

        <dependency>
            <groupId>hu.inbuss</groupId>
            <artifactId>thymeleaf-cdi</artifactId>
            <version>0.0.9</version>
        </dependency>

    or with Gradle:

    dependencies {
        compile 'hu.inbuss:thymeleaf-cdi:0.0.9'
    }
  2. When you invoke TemplateEngine.process(), you have to pass in an IContext object. Instead of constructing a new instance of Thymeleaf's WebContext class, get a reference to a CDIWebContextFactory bean (probably via dependency injection) and get a CDIWebContext instance from it. For example:

    import hu.inbuss.thymeleaf.cdi.CDIWebContextFactory;
    import javax.inject.Inject;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import org.thymeleaf.TemplateEngine;
    import org.thymeleaf.context.IWebContext;
    
    public class Example extends HttpServlet {
        @Inject private CDIWebContextFactory ctxFactory;
    
        public void doGet(HttpServletRequest req, HttpServletResponse resp) {
            final IWebContext ctx = ctxFactory.create(req, resp);
            final TemplateEngine engine = /* ... */;
            engine.process(/* template name */, ctx, resp.getWriter());
        }
    }

    (If your project was using plain Context instead of WebContext, replace it with a CDIContext instance obtained from the CDIContextFactory bean.)

  3. You're ready. Define CDI named beans, e.g.

    @javax.enterprise.context.SessionScoped
    @javax.inject.Named("cart")
    public class ShoppingCart implements java.io.Serializable {
        public int getItemCount() {
            /* ... */     
        }
        /* ... */
    }

    then reference them from a template:

    <span>You have <em th:text="${cart.itemCount}">many</em> products in your cart.</span>
hu.inbuss

INBUSS Kft.

Versions

Version
0.0.9
0.0.1