【问题标题】:Displaying the contents of a HashMap which contains Date objects in a dataTable in JSF/PrimeFaces在 JSF/PrimeFaces 的 dataTable 中显示包含 Date 对象的 HashMap 的内容
【发布时间】:2018-12-22 19:31:48
【问题描述】:

我创建了以下方法,它返回一个HashMap<Date, List<Date>>,其中键是Date 对象,值是Date 对象的List。该方法接受timeStampsList 并按天对它们进行分组。然后它返回上述HashMap 构造中的那些分组时间戳。

 public class GroupDatesByDay {

    HashMap<Date, List<Date>> groupedUserLogins = new HashMap<Date, List<Date>>();
    Calendar cal = Calendar.getInstance();

    public HashMap<Date, List<Date>> parseTimeStamps(List<Date> timeStamps) {

    DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss.SSS", Locale.US);
    List<Date> timeStamps = new ArrayList<Date>();

        for (Date ts : timeStamps) {

            cal.setTime(ts);
            cal.set(cal.HOUR_OF_DAY, 0);
            cal.set(cal.MINUTE, 0);
            cal.set(cal.SECOND, 0);
            cal.set(cal.MILLISECOND, 0);

            if (!groupedUserLogins.containsKey(cal.getTime())) {

                groupedUserLogins.put(cal.getTime(), new ArrayList<Date>());
            }
            groupedUserLogins.get(cal.getTime()).add(ts);
        }

        keySet = groupedUserLogins.keySet();
        keyList.addAll(keySet);
        return groupedUserLogins;
    }
}

HashMap 中的数据应如下所示:

Key                          List<Date> 

2018-07-11 
                  2018-07-11 08:14:08.540000 
                  2018-07-11 10:46:23.575000 

2018-07-12  
                  2018-07-12 12:51:48.928000 
                  2018-07-12 13:09:00.701000 
                  2018-07-12 16:04:45.890000 

2018-07-13 
                  2018-07-13 14:14:17.461000 

在我的 XHTML 中,我想在 dataTable 中显示这些数据,并通过 RowExpansion 来查看每天的各个时间戳。我编写了以下 XHTML。如您所见,userLogins 是我的 Java 方法返回的内容。我正在这个dataTable 中对其进行迭代,但我不知道如何以上述方式显示其中的值。

<p:dataTable var="userLogin" value="#{userEdit.userLogins}"class="DataTable">
        <p:column headerText=" Recent Logins">
          <h:outputLabel value="#{userLogin}">
          </h:outputLabel>
        </p:column>
</p:dataTable>

【问题讨论】:

  • 我省略了这一点,以使我的代码在 StackOverflow 上看起来更干净。问题在于实际上从 Hashmap 中获取这些值并显示它们。
  • 您的意思是要遍历列表?使用ui:repeat
  • 据此:stackoverflow.com/a/8552872/2242047 - 这是不可能的。请完整阅读我的问题。我正在尝试使用方括号来访问我的 HashMap 的内容。 dataTable / column 构造已经在迭代这些值。
  • 我知道你们已经离线了,但不要忘记在下面发布您的最终解决方案。我不想插话,但我通常避免在 JSF 中使用 Maps 并编写将映射值公开为列表的包装器组件。只是我的两分钱......

标签: java jsf primefaces hashmap java-7


【解决方案1】:

这是我最终解决的方法。正如 Melloware 所建议的,我必须加入一个列表。

我的实现略有改变:

private Hashtable<Date, List<Date>> groupedUserLogins = new Hashtable<Date, List<Date>>();
private Calendar cal = Calendar.getInstance();
private Set<Date> keySet = new TreeSet<Date>();
private List<Date> keyList = new ArrayList<Date>();

...

public Hashtable<Date, List<Date>> parseUserLogins(List<UserLogin> userLogins) {

        DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss.SSS", Locale.US);
        List<Date> timeStamps = new ArrayList<Date>();

        for (UserLogin u : userLogins) {
            timeStamps.add(u.getTimeStamp());
        }

        for (Date ts : timeStamps) {

            cal.setTime(ts);
            cal.set(cal.HOUR_OF_DAY, 0);
            cal.set(cal.MINUTE, 0);
            cal.set(cal.SECOND, 0);
            cal.set(cal.MILLISECOND, 0);
            dateFormat.format(ts);

            if (!groupedUserLogins.containsKey(cal.getTime())) {

                groupedUserLogins.put(cal.getTime(), new ArrayList<Date>());
            }
            groupedUserLogins.get(cal.getTime()).add(ts);
        }

        keySet = groupedUserLogins.keySet();
        keyList.addAll(keySet);
        return groupedUserLogins;
    }

我的 XHTML:

<p:dataTable var="loginDayDate" value="#{userEdit.keyList}"
    sortOrder="descending" sortBy="#{loginDayDate}" class="DataTable">

    <p:column width="15">
        <p:rowToggler />
    </p:column>

    <p:column headerText=" Recent Logins" width="110"
        sortBy="#{loginDayDate}">

        <h:outputLabel value="#{loginDayDate}">
            <f:convertDateTime pattern="#{Constants.DATE_FORMAT}" />

        </h:outputLabel>
    </p:column>
    <p:column headerText=" # of Logins" style="text-align : left">
        #{fn:length(userEdit.groupedUserLogins[loginDayDate])}
    </p:column>
    <p:rowExpansion>
        <p:dataTable var="specificDate"
            value="#{userEdit.groupedUserLogins[loginDayDate]}"
            class="DataTable">
            <p:column headerText="Time" style="text-align: left">
                <h:outputLabel value="#{specificDate}">
                    <f:convertDateTime pattern="#{Constants.DATETIME_FORMAT}" />
                </h:outputLabel>
            </p:column>
        </p:dataTable>
    </p:rowExpansion>
</p:dataTable>

外观:

【讨论】:

    猜你喜欢
    • 2012-02-22
    • 2011-12-04
    • 2017-07-16
    • 1970-01-01
    • 2012-05-17
    • 2014-12-10
    • 2011-12-13
    • 2018-10-28
    • 2015-08-27
    相关资源
    最近更新 更多