gpt4 book ai didi

java - 如何在 Java 中使用 RenderSnake 库生成 HTML 表格?

转载 作者:行者123 更新时间:2023-11-27 23:54:09 26 4
gpt4 key购买 nike

我正在尝试使用 Render Snake以编程方式为我生成 HTML 的 HTML 库。我正在尝试使用 Render Snake 制作 HTML 表格,如下所示 - 这只是我们如何使用 Render Snake 库制作表格的示例。

html
.table(class_("city-table"))
.tr()
.th().content("City")
.th().content("Country")
._tr()
.tr()
.td().content("Amsterdam")
.td().content("The Netherlands")
._tr()
._table();

我需要在迭代我的对象时做同样的事情并制作正确的 .tr 并关闭它。所以这就是让我很困惑的地方。

下面是我希望使用 RenderSnake 库将我的表放在 HTML 中的方式 - 这里 PoolName, TotalSyncCount, TotalAsyncCount, SyncNinetyFivePercentileAsyncNinetyFivePercentile 是我的列名,所以我为它们使用 th

PoolName    TotalSyncCount  TotalAsyncCount SyncNinetyFivePercentile    AsyncNinetyFivePercentile

Hello 100 100 4 0
World 300 300 2 0

下面是我的对象,它包含所有这些详细信息,我需要对其进行迭代以获取上述格式的表格

public class PoolMetrics {

private String poolName;
private String totalSyncCount;
private String totalAsyncCount;
private String syncNinetyFivePercentile;
private String asyncNinetyFivePercentile;

// getters and setters
}

到目前为止,我只能使用 RenderSnake 创建列名。我不确定如何通过迭代 PoolMetrics 对象在这些列名称中添加值 -

public static void main(String[] args) throws IOException {
List<PoolMetrics> poolMetricsList = new ArrayList<PoolMetrics>();

// here poolMetricsList has all the information as shown above in the table

HtmlCanvas html = new HtmlCanvas();
html.html().body().table().tr().th().content("PoolName").th().content("TotalSyncCount").th()
.content("TotalAsyncCount").th().content("SyncNinetyFivePercentile").th()
.content("AsyncNinetyFivePercentile")._tr()._table()._body()._html();

// now how do I iterate poolMetricsList to add values inside the column names
// as shown in the above table
}

问题陈述:-

如何使用 RenderSnake 迭代 poolMetricsList 对象并为这些列名称添加正确的值?我想生成我的 HTML,如上所示。

最佳答案

请记住,每个方法调用都会返回相同的 HtmlCanvas 实例(不是不可变的),因此每次调用都需要 tr()td()_html() 等在内部处理,您将返回当前实例。

这使链接变得很棒,但也不会要求您链接您的调用。

话虽如此,这里有一个完整的例子:

public class RendersnakeTest {

public static void main(String[] args) throws IOException {
List<PoolMetrics> poolMetricsList = new ArrayList<>();
poolMetricsList.add(new PoolMetrics("A", "0", "0", "0", "0"));
poolMetricsList.add(new PoolMetrics("A", "1", "1", "1", "1"));
poolMetricsList.add(new PoolMetrics("A", "2", "2", "2", "2"));
poolMetricsList.add(new PoolMetrics("A", "3", "3", "3", "3"));
poolMetricsList.add(new PoolMetrics("A", "4", "4", "4", "4"));

HtmlCanvas html = new HtmlCanvas();
html.html().body().table().tr().th().content("PoolName").th().content("TotalSyncCount").th()
.content("TotalAsyncCount").th().content("SyncNinetyFivePercentile").th()
.content("AsyncNinetyFivePercentile")._tr();

// add the rows
for (PoolMetrics pool : poolMetricsList) {
html.tr()
.td(class_("city-table")).content(pool.getPoolName())
.td().content(pool.getTotalAsyncCount())
.td().content(pool.getTotalSyncCount())
.td().content(pool.getSyncNinetyFivePercentile())
.td().content(pool.getAsyncNinetyFivePercentile())
._tr();
}

// close the table
html._table()._body()._html();

// write the file
final String rendered = html.toHtml();
final File output = new File("c:/output.html");
Files.write(output.toPath(), rendered.getBytes("UTF-8"), StandardOpenOption.TRUNCATE_EXISTING);
}

}

class PoolMetrics {

private String poolName;
private String totalSyncCount;
private String totalAsyncCount;
private String syncNinetyFivePercentile;
private String asyncNinetyFivePercentile;

public PoolMetrics(String poolName, String totalSyncCount, String totalAsyncCount, String syncNinetyFivePercentile, String asyncNinetyFivePercentile) {
this.poolName = poolName;
this.totalSyncCount = totalSyncCount;
this.totalAsyncCount = totalAsyncCount;
this.syncNinetyFivePercentile = syncNinetyFivePercentile;
this.asyncNinetyFivePercentile = asyncNinetyFivePercentile;
}

public String getPoolName() {
return poolName;
}

public String getTotalSyncCount() {
return totalSyncCount;
}

public String getTotalAsyncCount() {
return totalAsyncCount;
}

public String getSyncNinetyFivePercentile() {
return syncNinetyFivePercentile;
}

public String getAsyncNinetyFivePercentile() {
return asyncNinetyFivePercentile;
}
}

更新

class_ 方法由静态导入提供,它实际上包含在抽象类 HtmlAttributesFactory 中,要导入这些,请将以下内容添加到您的导入列表中:

import static org.rendersnake.HtmlAttributesFactory.*;

我已经更新了示例以包含此内容。

关于java - 如何在 Java 中使用 RenderSnake 库生成 HTML 表格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25074613/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com