2012-03-28 49 views
5

我想獲得輸出。Java中更好的Html生成器

<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> 

正如你可以看到它的混合的HTML,它會從Java邏輯生成其餘。 現在,這是我的問題 - 我如何在獨立的java(即不是jsp)中實現。我知道我可以把它寫到一個普通的文件中。但不知何故,我覺得這是一個醜陋的解決方案。是否有任何方法可以在一些更好的方式? 基本上我正在尋找一個適合java的好HTML生成器。 已經勾選-Freemarker。 另外我可以用任何語言實現,因爲java是我最喜歡的語言,所以我更喜歡它。

+0

你有沒有簽出「gagawa」? http://code.google.com/p/gagawa/ – 2012-03-28 14:42:52

+0

您可以使用簡單的模板引擎。檢查此條目:http://stackoverflow.com/questions/3793880/lightweight-template-engine-in-java – mdakin 2012-03-28 14:43:10

+0

*「我想得到低於輸出。」*該輸出格式不正確。使用[驗證服務](http://validator.w3.org/)進行檢查。 – 2012-03-28 14:44:01

回答

15

Gagawa「允許開發人員在Web或非Web應用程序中輕鬆動態地構建格式良好的HTML」。

它需要使用一個罐子和源代碼可供免費閱讀。

舉例...

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()); 

這將產生以下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> 
+0

在這種情況下完美的解決方案.thnx – Monojit 2012-03-28 20:22:56

5

內運行。如果你想堅持用純Java,您可以使用模板。

例如:

... constant html ... 
</tr> 
{variablePart} 
</tr> 
... constant html ... 

保存到某個地方(比如說,在.properties文件),並在您的應用String加載它。 然後讓你的普通建築規範...

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>"); 
    } 
} 

終於得到你的HTML:

String finalHTML = templateHTML.replace("{variablePart}", builder.toString()); 

它可能不是完美的,但它是一個有點比你有什麼更好的。

+0

它是一個更好的方法。我仍在探索。謝謝! – Monojit 2012-03-28 15:02:00

2

最有可能的是你想要的是一個templating engine。許多人存在,但兩個大男孩是FreemarkerApache Velocity。雙方都很高興在獨立的應用程序。你在文章中提到了Freemarker,但看起來好像你拒絕了它。我可以問爲什麼?

如果你不是在模板引擎之後,你可以用代碼構建DOM,可能使用javax.swing.text.Documentjavax.swing.text.html.HTMLEditorKit,但我會反對它。

+0

umm.I猜想學習曲線..;)現在探索gagawa,以後可能會檢查速度。 – Monojit 2012-03-28 20:24:53

+0

這是(海事組織)值得一邊學習曲線。在代碼中處理這些東西可能會很快變得醜陋。 – 2012-03-30 08:53:17

0

rythm是一種高性能的純Java模板(比速度快2〜3倍),其使用Razor像語法:

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

它支持用戶定義的佈局模板,用戶定義的標籤等等。結帳全功能演示http://play-rythm-demo.appspot.com/

1

Chunk是我的免費開源Java模板引擎。 Chunk就像Freemarker或Velocity,但語法更直觀。

組塊的嵌套的 「循環」 和 「如果」 的標記,使這種事情非常簡單:

my_template.chtml(發生在類路徑,例如,在SRC /主題/ 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> 

樣本Java與此模板的工作:

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(); 

我使用內聯表(一大塊方便的格式),以創建循環型數據,但你可以使用任何物品T的數組或列表帽子實現com.x5.util.DataCapsule和Chunk將在渲染模板之前將數據從對象中複製出來。

最終輸出:

<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> 
0

您也需要管理操作,在Java代碼中的CSS設置難以測試和變化,爲什麼不使用wicket.apache.org?