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)

1
  • Another option is to use JAXB's Fluent Plugin Commented Aug 28, 2010 at 14:01

3 Answers 3

34

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

Sign up to request clarification or add additional context in comments.

7 Comments

Looks good, are you still updating it?
Yep. I need to add HTML 5 soon. Probably add it in a couple of weeks. Its also now in the public maven repo.
Great job! It would be much better if the inner elements could go inside the constructors like: body(h1(), div(table(...)))
@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).
@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.
|
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.

4 Comments

This is definitely a step in the right direction (+1)
I think we have a winner. What a horrible non-friendly SEO project name :) Your right its probably not really fluent as it would involve a state machine. If anything its actually more like a Monad. Think JQuery.
It still seems clunky to me and doesn't support XHTML.
And it's now no longer being developed.
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.

2 Comments

The annoying probablem with Dom4J its lack of support of generics. ie node.elements() returns a List<?> not List<Element> But is my favorite XML library for Java
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.