I want to get below output.
<html>
<head>
</head>
<body>
<table>
<tbody>
<thead>
Blah Blah table Header--Constant Part
</thead>
<tr>
some text-constant part
</tr>
<!---Main Customization Part-->
for(i=0;i<some value;i++)
{
<tr>
for(j=0;j<another value;j++)
{
if(some condition)
{
<td class=another varibale>some text</td>
}
else
{
<td class=yet another varibale>some text</td>
}
}
</tr>
}
</body>
</html>
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.
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();
div.setId("mydiv").setCSSClass("myclass");
A link = new A();
link.setHref("http://www.example.com").setTarget("_blank");
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="http://www.example.com" target="_blank">
<img alt="some alt" src="some-image.png" class="frame" id="myimageid">
</a>
</div>
If you want to stick with pure java, you could use templates.
For instance:
... constant html ...
</tr>
{variablePart}
</tr>
... constant html ...
Save it somewhere (say, in a .properties
file) and load it in a String
in your app.
Then have your regular building code...
StringBuilder builder = new StringBuilder();
for(j=0;j<another value;j++)
{
if(some condition)
{
builder.append("<td class=another varibale>some text</td>");
}
else
{
builder.append("<td class=yet another varibale>some text</td>");
}
}
And finally get your HTML:
String finalHTML = templateHTML.replace("{variablePart}", builder.toString());
It might not be perfect, but it is a bit better than what you have.
You can consider to use velocity template from apache. Velocity is not required to run within Web or Application Server
How Velocity Works
User guide
Most likely what you want is a templating engine. Many exist but the two big boys are Freemarker and Apache Velocity. Both are happy in a stand alone application. You mentioned Freemarker in your post but it appears as if you have rejected it. Might I ask why?
If you are not after a templating engine you could build the DOM in code maybe using javax.swing.text.Document and javax.swing.text.html.HTMLEditorKit but I would recomemend against it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With