Monojit Monojit -5 years ago 273
HTML Question

Better Html Builder in java

I want to get below output.

Blah Blah table Header--Constant Part
some text-constant part
<!---Main Customization Part-->
for(i=0;i<some value;i++)
for(j=0;j<another value;j++)
if(some condition)
<td class=another varibale>some text</td>
<td class=yet another varibale>some text</td>

As you can see its a mixture of html and it will generate the rest from java logic.
Now here is my question-how can I implement in standalone java(i.e not jsp).I know I can write this to a normal file.But somehow I feel thats a ugly solution.Is there any way to get it done in some nicer way?
Basically I am looking for a good HTML builder for java.
Already checked-Freemarker.
Also I am open to implement in any language,As java is my favourite language,so I am prefering it.

Answer Source

Gagawa "allows developers to easily and dynamically build well-formed HTML in web or non-web applications".

It requires the use of one jar and the source code is freely available to peruse.

An example...

Div div = new Div();

A link = new A();

div.appendChild( link );

Img image = new Img( "some alt", "some-image.png" );
image.setCSSClass( "frame" ).setId( "myimageid" );
link.appendChild( image );

System.out.print( div.write() );

This produces the following HTML:

<div id="mydiv" class="myclass">
  <a href="" target="_blank">
   <img alt="some alt" src="some-image.png" class="frame" id="myimageid">
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download