Better Html Builder in java [closed]

限于喜欢 提交于 2019-12-04 13:09:03

问题


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.


回答1:


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>



回答2:


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.




回答3:


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




回答4:


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.




回答5:


Chunk is my free, open-source template engine for Java. Chunk is like Freemarker or Velocity, but with more intuitive syntax.

Chunk's nestable "loop" and "if" tags make this kind of thing very easy:

my_template.chtml (place in the classpath, eg in src/themes/my_template.chtml)

<html>
 <head>
 </head>
 <body>
 <table>
 <tbody>
 <thead>
 Blah Blah table Header--Constant Part
 </thead>
 <tr>
 some text-constant part
 </tr>
 {!---Main Customization Part--}
 {% loop in $list as $row %}
 <tr>
  {% loop in $row as $cell %}
   {% if ($cell.color == "blue") %}
        <td class="{$class_a}">{$cell.text}</td>
   {% else %}
        <td class="{$class_b}">{$cell.text}</td>
   {% endif %}
  {% endloop %}
 </tr>
 {% endloop %}
 </tbody>
 </table>
 </body>
</html>

Sample java for working with this template:

 import com.x5.template.Theme;
 import com.x5.template.Chunk;

 ...

 Theme theme = new Theme();
 Chunk html = theme.makeChunk("my_template");

 html.set("class_a", "blue_cell");
 html.set("class_b", "plain_cell");

 String row1 = "[[color,text],[blue,moe],[red,curly],[orange,larry]]";
 String row2 = "[[color,text],[red,hat],[black,dog],[blue,bottle]]";

 String[] list = new String[]{row1,row2};

 html.set("list", list);

 out = getOutputWriter();
 html.render( out );  // or System.out.print( html.toString() )

 out.flush();
 out.close();

I used inline tables (a Chunk convenience format) to create loop-friendly data but you can use arrays or lists of any object that implements com.x5.util.DataCapsule and Chunk will copy the data right out of your objects before rendering the template.

Final output:

<html>
 <head>
 </head>
 <body>
 <table>
 <tbody>
 <thead>
 Blah Blah table Header--Constant Part
 </thead>
 <tr>
 some text-constant part
 </tr>
 <tr>
        <td class="blue_cell">moe</td>
        <td class="plain_cell">curly</td>
        <td class="plain_cell">larry</td>
 </tr>
 <tr>
        <td class="plain_cell">hat</td>
        <td class="plain_cell">dog</td>
        <td class="blue_cell">bottle</td>
 </tr>
 </tbody>
 </table>
 </body>
</html>



回答6:


rythm is a high performance (2 to 3 times faster than velocity) pure Java template which use Razor like syntax:

@args String who
<p>
@if ("world".equals(who)) {
  hello @who
} else {
  bye @who
}
</p>

It supports user defined layout template, user defined tag and a lot more. Checkout the full feature demo at http://play-rythm-demo.appspot.com/




回答7:


You need also manage actions ,Css set in java code is hard to test and change ,Why not use wicket.apache.org ?



来源:https://stackoverflow.com/questions/9909801/better-html-builder-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!