Fork me on GitHub
j2html logo

Reclaim control over your HTML

Like j2html? Let others know: Share: Twitter | Reddit | Facebook | Google+

Basic example

Creating a basic webpage in j2html is pretty similar to HTML. This Java code:

html().with(
    head().with(
        title("Title"),
        link().withRel("stylesheet").withHref("/css/main.css")
    ),
    body().with(
        main().with(
            h1("Heading!")
        )
    )
)

Becomes this HTML:

<html>
    <head>
        <title>Title</title>
        <link rel="stylesheet" href="/css/main.css">
    </head>
    <body>
        <main>
            <h1>Heading!</h1>
        </main>
    </body>
<html>

It's literally impossible to forget to close a div, mistype an attribute name, or forget an attribute quote! Remember to include the Java wrapping code though, j2html is not a template language, all files are .java. To see how the wrapping code could look, check out the getting started example.

New in j2html 0.7:
j2html 0.7 introduces a file-api, described fully on the news page.

Partials

You can create partials for elements you use a lot:

public static Tag enterPasswordInput(String placeholder) {
    return passwordInput("enterPassword", placeholder);
}

public static Tag choosePasswordInput(String placeholder) {
    return passwordInput("choosePassword", placeholder);
}

public static Tag repeatPasswordInput(String placeholder) {
    return passwordInput("repeatPassword", placeholder);
}

public static Tag passwordInput(String identifier, String placeholder) {
    return input()
        .withType("password")
        .withId(identifier)
        .withName(identifier)
        .withPlaceholder(placeholder)
        .isRequired();
}

public static Tag emailInput(String placeholder) {
    return input()
        .withType("email")
        .withId("email")
        .withName("email")
        .withPlaceholder(placeholder)
        .isRequired();
}

public static Tag submitButton(String text) {
    return button().withType("submit").withText(text);
}

The equivalent HTML would be:

<input
    type="password"
    id="enterPassword"
    name="enterPassword"
    placeholder="Enter password"
    required
    >

<input
    type="password"
    id="choosePassword"
    name="choosePassword"
    placeholder="Choose password"
    required
    >

<input
    type="password"
    id="repeatPassword"
    name="repeatPassword"
    placeholder="Repeat password"
    required
    >

<input
    type="email"
    id="email"
    name="email"
    placeholder="Email"
    required
    >

<button type="submit">Text</button>

You can then use these partials, for example in a registration form:

h1("Please sign up"),
form().withMethod("post").with(
    emailInput("Email address"),
    choosePasswordInput("Choose Password"),
    repeatPasswordInput("Repeat Password"),
    submitButton("Sign up")
)

Pretty clean, right? The rendered HTML is more verbose:

<h1>Please sign up</h1>
<form method="post">
    <input type="email" id="email" name="email" placeholder="Email address" required>
    <input type="password" id="choosePassword" name="choosePassword" placeholder="Choose password" required>
    <input type="password" id="repeatPassword" name="repeatPassword" placeholder="Repeat password" required>
    <button type="submit">Sign up</button>
</form>

Imagine if you wanted labels in addition. The Java snippet would look almost identical: You could create a partial called passwordAndLabel() and nothing but the method name would change. The resulting HTML however, would be twice or thrice as big, depending on whether or not you wrapped the input and label in another tag.

Loops

Using Java 8's lambda syntax, you can write loops (via streams) inside your HTML-builder:

body().with(
    div().withId("employees").with(
        employees.stream().map(employee ->
            div().withClass("employee").with(
                h2(employee.getName()),
                img().withSrc(employee.getImgPath()),
                p(employee.getTitle())
            )
        ).collect(Collectors.toList())
    )
)

j2html also offers a custom each method, which is slightly more powerful:

// each() lets you iterate through a collection and returns the generated HTML
// as a DomContent object, meaning you can add siblings, which is not possible
// using the stream api in the previous example
body().with(
    div().withId("employees").with(
        p("Some sibling element"),
        each(employees, employee ->
            div().withClass("employee").with(
                h2(employee.getName()),
                img().withSrc(employee.getImgPath()),
                p(employee.getTitle())
            )
        )
    )
)

If you need to filter your collection, j2html has a built in filter function too:

// filter() is meant to be used with each(). It just calls the normal
// stream().filter() method, but hides all the boilerplate Java code
// to keep your j2html code neat
body().with(
    div().withId("employees").with(
        p("Some sibling element"),
        each(filter(employees, employee -> employee != null), employee ->
            div().withClass("employee").with(
                h2(employee.getName()),
                img().withSrc(employee.getImgPath()),
                p(employee.getTitle())
            )
        )
    )
)

If Java8 and streams are not your thing, you can always extract a method:

List<DomContent> createEmployeeList(List<Employee> employeeList) {
    List<DomContent> domContent = new ArrayList<>();
    for(Employee employee : employeeList) {
        domContent.add(
            div().withClass("employee").with(
                h2(employee.getName()),
                img().withSrc(employee.getImgPath()),
                p(employee.getTitle())
            )
        );
    }
    return domContent;
}

//call method in your builder
body().with(
    div().withId("employees").with(
        createEmployeeList(employees)
    )
)

Since this is pure Java, all the Employee methods (getName, getImgPath, getTitle) are available to you, and you get autocomplete suggestions and compile time errors.

Given three random employees, all the above approaches would give the same HTML:

<body>
    <div id="employees">
        <div class="employee">
            <h2>David</h2>
            <img src="/img/david.png">
            <p>Creator of Bad Libraries</p>
        </div>
        <div class="employee">
            <h2>Christian</h2>
            <img src="/img/christian.png">
            <p>Fanboi of Jenkins</p>
        </div>
        <div class="employee">
            <h2>Paul</h2>
            <img src="/img/paul.png">
            <p>Hater of Lambda Expressions</p>
        </div>
    </div>
</body>

Dynamic views

Once you've set up partials, you can call them from wherever, which greatly reduces potential errors. Let's say we want to include the form from the partials-example in our webpage. We want a header above and a footer below. A lot of templating languages make you do this:

#include("/path/to/header")
#setTitle("Signup page")
<h1>Please sign up</h1>
<form>
...
</form>
#include("/path/to/footer")

This is a pain to work with. You have no idea what the header and footer expects, and you have no way to affect how they treat your content. You can easily break the site by forgetting to close divs, or by forgetting to include either the header or the footer in one of your views. In j2html you can specify the context in which a view is rendered, and supply the rendering method with type safe parameters! If we want to insert our form in a header/footer frame, we simply create a MainView and make it take our view as an argument:

public class MainView {
    public static String render(String pageTitle, Tag... tags) {
        return html().with(
            head().with(
                title(pageTitle)
            ),
            body().with(
                header().with(
                    ...
                ),
                main().with(
                    tags //the view from the partials example
                ),
                footer().with(
                    ...
                )
            )
        ).render();
    }
}

MainView.render(
    "Signup page",
    h1("Please sign up"),
    form().withMethod("post").with(
        emailInput("Email address"),
        choosePasswordInput("Choose Password"),
        repeatPasswordInput("Repeat Password"),
        submitButton("Sign up")
    )
);

Which will result in the rendered HTML:

<html>
    <head>
        <title>Signup page</title>
    </head>
    <body>
    <header>
        ...
    </header>
    <main>
        <h1>Please sign up</h1>
        <form method="post">
            <input type="email" id="email" name="email" placeholder="Email address" required>
            <input type="password" id="choosePassword" name="choosePassword" placeholder="Choose password" required>
            <input type="password" id="repeatPassword" name="repeatPassword" placeholder="Repeat password" required>
            <button type="submit">Sign up</button>
        </form>
    </main>
    <footer>
        ...
    </footer>
    </body>
</html>

We would now get a compilation error if we forgot to include a title, and there is 0 chance of forgetting either header or footer, mistyping paths, forgetting to close divs, or anything else.