j2html Documentation

repository·master·Indexed 21 days ago

https://github.com/tipsy/j2html

A Java library for typesafe HTML generation that provides a fluent API to build HTML structures instead of using manual string concatenation. Includes instructions for dependency management via Maven and Gradle, and usage of TagCreator for rendering HTML.

Tokens
569
Snippets
4
Records
5
Agent score
24%

What's inside j2html

  1. Build HTML using TagCreator

    master

    You can generate typesafe HTML by importing j2html.TagCreator.* statically. Use the tag methods (like body, h1, img) to compose your HTML structure. For elements that require attributes, use the .with[AttributeName]() pattern. Finally, call .render() to produce the HTML string.

    import static j2html.TagCreator.*;
    
    public class Main {
        public static void main(String[] args) {
            body(
                h1("Hello, World!"),
                img().withSrc("/img/hello.png")
            ).render();
        }
    }
  2. Deploy the j2html website

    master

    To deploy the website, you must update the static files located in the j2html/docs directory. You can do this by manually downloading the files from your local running instance or by using curl to save the output of specific URLs to their respective filenames.

    curl "http://localhost:8888/" > index.html
    curl "http://localhost:8888/download.html" > download.html
    curl "http://localhost:8888/examples.html" > examples.html
    curl "http://localhost:8888/news.html" > news.html
    curl "http://localhost:8888/404.html" > 404.html
  3. Add the j2html dependency via Maven

    master

    To use j2html in your Java project, add the following dependency to your pom.xml file.

    <dependency>
        <groupId>com.j2html</groupId>
        <artifactId>j2html</artifactId>
        <version>1.6.0</version>
    </dependency>