如何正确从 HashMap 加载到 HTML 表格?

huangapple go评论82阅读模式
英文:

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&lt;String&gt; names = new ArrayList&lt;&gt;();           
ArrayList&lt;String&gt; emails = new ArrayList&lt;&gt;();
HashMap&lt;Integer, ArrayList&lt;String&gt;&gt; idAndDateMap = new HashMap&lt;&gt;();
StringBuilder sB = new StringBuilder();
sB.append(
&quot;&lt;!DOCTYPE html&gt;\n&quot; +
&quot;&lt;html lang=\&quot;en\&quot;&gt;\n&quot; +
&quot;&lt;head&gt;\n&quot; +
&quot;    &lt;meta charset=\&quot;UTF-8\&quot;&gt;\n&quot; +
&quot;    &lt;title&gt;Registered vacations&lt;/title&gt;\n&quot; +
&quot;&lt;/head&gt;\n&quot; +
&quot;&lt;body&gt;\n&quot; +
&quot;&lt;H2&gt;Registered Vacations&lt;/H2&gt;\n&quot; +
&quot;&lt;table border=\&quot;5\&quot; cellpadding=\&quot;10 \&quot; cellspacing=\&quot;0 \&quot;&gt;\n&quot; +
&quot;&lt;tr&gt;\n&quot; +
//header id                &quot;&lt;th&gt; &quot; + resMetaPersoner.getColumnName(1) + &quot; &lt;/th&gt;\n&quot; +   
//header name                 &quot;&lt;th&gt;&quot; + resMetaPersoner.getColumnName(2) + &quot;&lt;/th&gt;\n&quot; +     
//header email                &quot;&lt;th&gt;&quot; + resMetaPersoner.getColumnName(3) + &quot;&lt;/th&gt;\n&quot; +     
//header date       &quot;&lt;th colspan = \&quot;25\&quot;&gt;&quot; + resMetaDato.getColumnName(2) + &quot;&lt;/th&gt;\n&quot; +
&quot;&lt;/tr&gt;\n&quot;);
sB.append(&quot;&lt;tr&gt;&quot;);
idAndDateMap.entrySet().forEach(entry -&gt; {
sB.append(&quot;&lt;td&gt;&quot; + entry.getKey() + &quot;&lt;/td&gt;&quot;);    //the ids
}
for (int i = 0; i &lt; names.size(); i++) {
sB.append(
&quot;&lt;td&gt;&quot; + names.get(i) + &quot;&lt;/td&gt;&quot; +      //names
&quot;&lt;td&gt;&quot; + emails.get(i) + &quot;&lt;/td&gt;&quot;);     //emails
}
idAndDateMap.entrySet().forEach(entry -&gt; {
sB.append(&quot;&lt;td&gt;&quot; + entry.getValue() + &quot;&lt;/td&gt;&quot;);    //the the dates
}
sB.append(&quot;&lt;/tr&gt;\n&quot;);
}
sB.append(&quot;&lt;/table&gt;\n&quot;
+ &quot;&lt;/body&gt;\n&quot;
+ &quot;&lt;/html&gt;&quot;);
File htmlTemplateFile = new File(
&quot;C:\\Users\\Rosso\\Desktop\\sysco\\newVaca\\src\\main\\frontend\\index.html&quot;);
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&lt;String&gt; dates;
public Person(int id, String name, String email, ArrayList&lt;String&gt; 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&lt;String&gt; dates) {
this.dates = dates;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public ArrayList&lt;String&gt; 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 &lt; personList.size(); i++) {
Person p = personList.get(i);
sB.append(&quot;&lt;tr&gt;&quot; +
&quot;&lt;td&gt;&quot; + p.getId() + &quot;&lt;/td&gt;&quot; +
&quot;&lt;td&gt;&quot; + p.getName() + &quot;&lt;/td&gt;&quot; +
&quot;&lt;td&gt;&quot; + p.getEmail() + &quot;&lt;/td&gt;&quot; +
&quot;&lt;td&gt;&quot; + p.getDates() + &quot;&lt;/td&gt;&quot; +
&quot;&lt;/tr&gt;\n&quot;);
}
sB.append(&quot;&lt;/table&gt;\n&quot;
+ &quot;&lt;/body&gt;\n&quot;
+ &quot;&lt;/html&gt;&quot;);

huangapple
  • 本文由 发表于 2020年9月2日 14:23:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/63699785.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定