如何解决如何从HashMap正确加载到html表?
我正在创建一个假期登记应用程序。 我有一个id(key)的HashMap和属于每个id(values)的日期列表。我在表中的输出必须是:
1 |名称1 | email1 | date1 | date2 | date3等...
2 | name2 | email3 | date1 | date2 | date3 | date4等...
如何引用每个日期来更正ID?
我的代码:
ArrayList<String> names = new ArrayList<>();
ArrayList<String> emails = new ArrayList<>();
HashMap<Integer,ArrayList<String>> idAndDateMap = new HashMap<>();
StringBuilder sB = new StringBuilder();
sB.append(
"<!DOCTYPE html>\n" +
"<html lang=\"en\">\n" +
"<head>\n" +
" <Meta charset=\"UTF-8\">\n" +
" <title>Registered vacations</title>\n" +
"</head>\n" +
"<body>\n" +
"<H2>Registered Vacations</H2>\n" +
"<table border=\"5\" cellpadding=\"10 \" cellspacing=\"0 \">\n" +
"<tr>\n" +
//header id "<th> " + resMetaPersoner.getColumnName(1) + " </th>\n" +
//header name "<th>" + resMetaPersoner.getColumnName(2) + "</th>\n" +
//header email "<th>" + resMetaPersoner.getColumnName(3) + "</th>\n" +
//header date "<th colspan = \"25\">" + resMetaDato.getColumnName(2) + "</th>\n" +
"</tr>\n");
sB.append("<tr>");
idAndDateMap.entrySet().forEach(entry -> {
sB.append("<td>" + entry.getKey() + "</td>"); //the ids
}
for (int i = 0; i < names.size(); i++) {
sB.append(
"<td>" + names.get(i) + "</td>" + //names
"<td>" + emails.get(i) + "</td>"); //emails
}
idAndDateMap.entrySet().forEach(entry -> {
sB.append("<td>" + entry.getValue() + "</td>"); //the the dates
}
sB.append("</tr>\n");
}
sB.append("</table>\n"
+ "</body>\n"
+ "</html>");
File htmlTemplateFile = new File(
"C:\\Users\\Rosso\\Desktop\\sysco\\newVaca\\src\\main\\frontend\\index.html");
FileUtils.writeStringToFile(htmlTemplateFile,sB.toString());
FileUtils.readFiletoString(htmlTemplateFile);
解决方法
通过创建单独的类“人”来解决:
import java.util.ArrayList;
public class Person {
private int id;
private String name;
private String email;
private ArrayList<String> dates;
public Person(int id,String name,String email,ArrayList<String> dates){
setId(id);
setName(name);
setEmail(email);
setDates(dates);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public void setDates(ArrayList<String> dates) {
this.dates = dates;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public ArrayList<String> getDates() {
return dates;
}
在主班:
while (resultSetPersons.next()) {
int id = resultSetPersons.getInt(1);
String name = resultSetPersons.getString(2);
String email = resultSetPersons.getString(3);
setPerson(id,name,email);
}
while (resultSetDates.next()) {
int foreignKey = resultSetDates.getInt(1);
String foreignKeyString = String.valueOf(foreignKey);
String date = resultSetDates.getString(2);
addDates(foreignKey,date);
}
输出到html表:
for (int i = 0; i < personList.size(); i++) {
Person p = personList.get(i);
sB.append("<tr>" +
"<td>" + p.getId() + "</td>" +
"<td>" + p.getName() + "</td>" +
"<td>" + p.getEmail() + "</td>" +
"<td>" + p.getDates() + "</td>" +
"</tr>\n");
}
sB.append("</table>\n"
+ "</body>\n"
+ "</html>");
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。