Fork me on GitHub

Getting started with j2html

Import TagCreator to get started. j2html's syntax is fluent and closely matched to HTML:

import static j2html.TagCreator.*;

public class Main {
    public static void main(String[] args) {
        body(
            h1("Hello, World!"),
            img().withSrc("/img/hello.png")
        ).render();
    }
}

The Java code above becomes the HTML below:

<body>
    <h1>Hello, World!</h1>
    <img src="/img/hello.png">
</body>

Check out some more examples.

Intended use

Consider using j2html if:

Be careful about using j2html if:

Why did you make this library?

First: j2html is a Java HTML builder. It's not a template engine, and it doesn't want to compete with template engines. We were looking for a good way to create HTML for a complex login solution which had many different forms (with different configurations, depending on user state and user actions, etc.), but very little actual HTML per page. The result was j2html. We decided to release the Java HTML builder we made, since it seems better than all the other Java HTML builders we found while researching the subject. Hopefully someone will find it useful!

Want a simple and modern web framework?
Try our other project: https://javalin.io