Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Better Html Builder in java [closed]

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.

like image 883
Monojit Avatar asked Mar 28 '12 14:03

Monojit


4 Answers

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>
like image 190
Zack Macomber Avatar answered Sep 29 '22 03:09

Zack Macomber


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.

like image 32
Marcelo Avatar answered Sep 29 '22 03:09

Marcelo


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

like image 45
Pau Kiat Wee Avatar answered Sep 29 '22 02:09

Pau Kiat Wee


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.

like image 24
Michael Lloyd Lee mlk Avatar answered Sep 29 '22 02:09

Michael Lloyd Lee mlk