在Java中更好的Html生成器 [英] Better Html Builder in java
问题描述
< html>
< head>
< / head>
< body>
< table>
< tbody>
< thead>
Blah Blah表头 - 常量部分
< / thead>
< tr>
一些文本常量部分
< / tr>
<!--- ---主要定制零件 - >
for(i = 0; i< some value; i ++)
{
< tr>
for(j = 0; j {
if(some condition)
{
< td class = another varibale>一些文字< ; / TD>
}
else
{
< td class =另一个varibale>一些文字< / td>
}
}
< / tr>
}
< / body>
< / html>
正如您可以看到它的一个混合的html,它将从java逻辑中生成剩余的。
现在这里是我的问题 - 我如何在独立的java(即不是jsp)中实现。我知道我可以把它写到一个普通的文件中。但不知何故,我觉得这是一个丑陋的解决方案。是否有任何方法可以得到它以更好的方式完成?
基本上我正在为java寻找一个好的HTML生成器。
已经勾选 - Freemarker。
我也可以用任何语言实现,因为java是我最喜欢的语言,所以我更喜欢它。
它需要使用一个jar,并且可以免费阅读源代码。
一个例子...
Div div = new Div();
div.setId(mydiv)。setCSSClass(myclass);
A link = new A();
link.setHref(http://www.example.com).setTarget(_blank);
div.appendChild(link);
图片=新图片(some alt,some-image.png);
image.setCSSClass(frame).setId(myimageid);
link.appendChild(image);
System.out.print(div.write());
这会生成以下HTML:
< div id =mydivclass =myclass>
< a href =http://www.example.comtarget =_ blank>
< img alt =某些altsrc =some-image.pngclass =frameid =myimageid>
< / a>
< / div>
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>
这篇关于在Java中更好的Html生成器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!