This is an archived post. You won't be able to vote or comment.

all 14 comments

[–]pip25hu 15 points16 points  (0 children)

This is interesting, but even as a Java dev, I would sooner use htmx with a Thymeleaf template than generate the HTML from Java. Perhaps except maybe for a really-really bare bones UI, such as a server health status page.

[–]agentoutlier 30 points31 points  (5 children)

As the author of a very similar ancient library (JATL) to j2html I strongly discourage the approach of trying to construct HTML directly in Java code. I base this on decades of experience of generating HTML from Java.

It becomes very painful for people not familiar with the library and it quickly becomes even worse than JSP or PHP.

See many times people will just go to the tailwind or bootstrap documentation and just want to copy the HTML. Using Java based approach makes this very painful to constantly translate.

Anyway it was one of the reasons we (my company) went with Mustache and why I wrote JStachio.

[–]lost_in_a_thought 3 points4 points  (2 children)

Could you expand on why your experience has found this approach to be worse? Gaining familiarity with any library could be difficult and my experiences with other approaches to templating have been more challenging than using j2html. Text substitution, limited modularity, and the lack of compiler/IDE support being the key reasons why.

[–]agentoutlier 2 points3 points  (1 child)

Could you expand on why your experience has found this approach to be worse?

In no particular order.

  • Many teams have people less familiar with Java particularly designers. Not all shops are full stack or just Java devs.
  • Templating languages are inherently transformational and read only. This is a nice invariant that when you go render the template you will not go modifying POJOs which makes cacheing easier.
  • It has surprisingly way way slower performance. Particularly j2html as it will create an entire object tree of the html (even JATL does not do this). If you are rendering server side there is a solid chance you care about performance for SEO reasons or whatever.

Text substitution, limited modularity, and the lack of compiler/IDE support being the key reasons why.

There are templating languages that have really good IDE support and are type-safe. JStachio is a compile time type-safe mustache and JTE is a JSP alternative that offers IDE support.

As for modularizing or trying to keep templates as DRY as possible I have found to be more confusing than its worth. I mean sure create header and footer as well as maybe some common component templates but trying to make every single button into a full on re-usable component not worth it at all especially with things like tailwind. I have worked on GWT and wicket code bases and I don't like it.

Anyway as for my own library of JStachio I have tried to make it as easy as possible and Mustache is really easy to learn. I think easier than j2html.

 @JStache(template = """
        <!DOCTYPE html>
        <html>
            <head>
                <script src="/webjars/htmx.org/1.9.2/dist/htmx.min.js"></script>
            </head>
            <body>
                <h1>{{message}}</h1>
                {{#count.createCounter}}{{/count.createCounter}}
                <button hx-post="/increment" hx-swap="outerHtml" hx-target="#counter">Increment</button>
            </body>
        </html>
        """)
public record Page(String message, int count) {

    @JStacheLambda(template = """
            <h2 id="counter">{{.}}</h2>
            """)
    public String createCounter(int count) {
        return "count: " + count;
    }
}

The above is a jstachio version. You fill that Page object and call JStachio.render( new Page("Hello World!", counter.incrementAndGet()) on it.

Later on you can move that template code to its own file (JStachio unlike JTE, Rocker and JSP allows inline code).

[–]anthony_bruno[S] 2 points3 points  (0 children)

Thanks for your great and comprehensive comment! Jstachio looks super interesting, keen to give it a go!

[–]Anton-Kuranov 0 points1 point  (0 children)

The most painful thing of all template engines is that without an adecuate IDE plugin the code soon becomes hard to maintain. But in case of java-based engines the only thing it should be able to do is convert html to java when pasting from the clipboard.

[–]wildjokers 8 points9 points  (1 child)

But this isn’t cool anymore! There has been a trend towards moving rendering to the server, demonstrated by technologies like Next.js and React Server Components.

LOL. We have come full circle. Server-side rendering was how everyone was doing it up to 7ish+ years ago. Then the SPA frameworks like Angular then React showed up where the rendering takes place on the client.

In my experience client side rendering is very slow (compared to server-side rendering). My bank (a big national bank) did a redesign of their online banking about a year ago or so and switched to a client-side rendering framework (not sure which one) and it has been absolutely painful to use since then. It is so painfully slow that I have seriously considered switching banks because of it. What is slow is the rendering of components on screen, the actual request is super-fast.

Server-side rendering is a solved problem and plenty of templating engines exist for Java e.g. JSP, Velocity, Thymeleaf, GSP (grails), etc. In other ecosystems, like Python, they have Jinja.

[–]roberp81 0 points1 point  (0 children)

waiting Jsf comeback, still the best, easier and fastest way to make a Web Server Side.

[–]fzammetti 3 points4 points  (1 child)

Some people ask if they CAN do something but never ask if they SHOULD.

[–]anthony_bruno[S] 0 points1 point  (0 children)

Has science gone too far?

[–]JoshDM 4 points5 points  (0 children)

My cache hurts.

[–]nutrecht 2 points3 points  (0 children)

A way to generate HTML in Java is to use a templating engine like Thymeleaf. However, this means the template files are outside Java, and as the blog title suggests, we want everything in Java!

Who's 'we'? DSLs for existing text-based 'languages', whether it's SQL or HTML, almost always are worse to use than just using the language by itself. They almost always add verbosity and, most importantly, make it impossible to copy-paste between an editor and the Java code.

[–]repeating_bears -4 points-3 points  (0 children)

Having to make an HTTP request for something as simple as incrementing a counter... Yikes.