【问题标题】:how to load to html table from HashMap correctly?如何正确从 HashMap 加载到 html 表?
【发布时间】:2020-09-02 06:23:00
【问题描述】:

我正在创建一个假期登记应用程序。 我有一个 ids(key) 的 HashMap 和属于每个 id(values) 的日期列表。我的表格输出需要是:

身份证 | 姓名 | 电子邮件 | 日期

1 |名称1 |电子邮件1 |日期1 |日期2 | date3 等...

2 |名称2 |电子邮件3 |日期1 |日期2 |日期3 | 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);

【问题讨论】:

  • 您可能希望创建一个包含nameemailList&lt;Date&gt; 的自定义类,而不是ArrayList&lt;String&gt;。那么您的值将是 ArrayList&lt;Passenger&gt; 或您选择调用该类的任何值。
  • 谢谢我试试看!
  • 因为您的建议,我花了 30 分钟才解决这个问题!谢谢大哥!
  • @Gryphon 我如何在堆栈中给你信用?
  • 不用担心 - 您可以将最终解决方案(或您觉得舒服的多少)放入答案中,并将其标记为答案。很高兴它对你有用 =)

标签: java hashmap stringbuilder fileutils


【解决方案1】:

通过创建一个单独的类“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>");

【讨论】:

    猜你喜欢
    • 2016-11-16
    • 1970-01-01
    • 1970-01-01
    • 2019-03-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-27
    • 2019-11-27
    • 2011-04-08
    相关资源
    最近更新 更多