英文:
how to load to html table from HashMap correctly?
问题
我正在创建一个假期登记应用程序。
我有一个包含ID(键)和属于每个ID的日期列表(值)的哈希映射。我需要从表格中输出:
Id | name | email | Date
1 | name1 | 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" +
"<th>Id</th>\n" +
"<th>Name</th>\n" +
"<th>Email</th>\n" +
"<th colspan=\"25\">Date</th>\n" +
"</tr>\n");
sB.append("<tr>");
idAndDateMap.entrySet().forEach(entry -> {
sB.append("<td>" + entry.getKey() + "</td>");
});
for (int i = 0; i < names.size(); i++) {
sB.append(
"<td>" + names.get(i) + "</td>" + // 姓名
"<td>" + emails.get(i) + "</td>"); // 邮箱
}
idAndDateMap.entrySet().forEach(entry -> {
sB.append("<td>" + entry.getValue() + "</td>"); // 日期
});
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);
注意:以上只是你提供的代码的翻译部分,用于将代码注释和字符串内容翻译成中文。
英文:
I am creating a vacation register app.
I have a HashMap of ids(key) and list of dates belonging to each id(values). My output from the table needs to be :
Id | name | email | Date
1 | name1 | email1 | date1 | date2 | date3 etc...
2 | name2 | email3 | date1 | date2 | date3 | date4 etc...
How do i referr each date to correct Id??
my code :
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);
答案1
得分: 0
以下是已经翻译好的内容:
通过创建一个独立的类'Person'来解决:
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>");
英文:
solved by creating a seperate class 'Person':
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;
}
In main class:
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);
}
output to html Table:
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>");
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论