32

I'm always looking for a modern Java library that makes creating valid (X)HTML snippets easy.

Yes you could use a templating language but there are times when you do not want to do this because Java has some advantages over insert your favorite templating language.

I have seen lots of in-house HTML builders in many projects but there is no Commons-HTML Builder that I can find.

Does anyone know of one?

It would be ideal if it took advantage of the Java 5/6/7 type system (generics) and support Fluent Style. Or something like fluent style ie JQuery style chaining, or a state machine used in mocking libraries like JMock (pedantically speaking a Monad).

A rough builder example might be:

new Html().title("stuff").body().in().div().in().h1("Hello World").hr();

Another example: http://codemonkeyism.com/the-best-markup-builder-i-could-build-in-java/

I ended up writing my own: Java Anti-template Language (JATL)

Adam Gent
  • 47,843
  • 23
  • 153
  • 203

3 Answers3

34

I ended up writing my own library called Java Anti-template Language (JATL)

ZeroOne
  • 3,041
  • 3
  • 31
  • 52
Adam Gent
  • 47,843
  • 23
  • 153
  • 203
  • Looks good, are you still updating it? – C. Ross Mar 29 '11 at 13:10
  • Yep. I need to add HTML 5 soon. Probably add it in a couple of weeks. Its also now in the public maven repo. – Adam Gent Mar 31 '11 at 15:38
  • 2
    Great job! It would be much better if the inner elements could go inside the constructors like: `body(h1(), div(table(...)))` – Lenik Sep 26 '11 at 09:27
  • @XieJilei Yeah I was thinking of adding that in the major release for people that like lisp style S-expressions. There are some evaluation challenges in going that direction (ie you want the body tag to be written before the h1 tag... otherwise you will have to buffer the whole document). – Adam Gent Oct 18 '11 at 17:09
  • There is also https://github.com/emicklei/rendersnake and http://j2html.com/ – ed22 Apr 05 '16 at 04:16
  • 1
    @ed22 yes JATL is looking pretty dated compared to these newer kids with their fancy web pages :) . j2html looks like it is not streaming and thus it might have memory issues if you giant documents (see my previous comments on S-expressions). rendersnake looks very similar to JATL albeit with far better code generator and use of types at the loss of simplicity and extendability. Funny thing is I use JATL more for non HTML markup (ie XML) these days of which both newer libraries don't seem to support. – Adam Gent Apr 06 '16 at 00:32
  • 404 not found, do you have an alternative link? – Ferrybig Jun 21 '19 at 08:41
3

Have you tried the Jakarta Element Construction Set (ECS) project?

It is not really a fluent API - reminds me more of StringBuilder than Mockito... But functionally I think it is what you're after.

serg10
  • 31,923
  • 16
  • 73
  • 94
3

Your best bet is probably to use an XML library and render the output as HTML.

I.E. Dom4J defines a HtmlWriter class for HTML-specific XML output.

But you'd still have to create your own api on top of it to actually create the document.

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
  • The annoying probablem with Dom4J its lack of support of generics. ie node.elements() returns a List> not List But is my favorite XML library for Java – Adam Gent Aug 27 '10 at 12:08
  • I would prefer to print HTML directly and escape all variables by hand, instead of any XML library. To do with HTML, XML library can make code ugly and extreme long. – Lenik Sep 26 '11 at 09:31