b.roth b.roth - 7 days ago 6
Java Question

How to generate HTML code programatically in Java?

Is there any recommended Java library for generating HTML code programatically?

For example, I'd like to do something like this:

// Creates table with 1 row and 2 columns
HTMLTable table = new HTMLBuilder().getHTMLTable(1, 2);

// Sets the table contents
table.setCell(0,0, "abc");
table.setCell(0,1, "def");

String html = table.getHTML();


HTMLTable
and
HTMLBuilder
are classes that the library would provide.

Answer

There's the Apache Jakarta ECS library. It claims to be HTML 4.0 compatible. Table Javadoc here.

Another solution may be to use a templating library. Freemarker is particularly good (and powerful). Velocity is less powerful but perhaps simpler.

Comments