如何在Struts2中使用迭代器从linkedhashmap中检索值......?

发布于 2024-11-10 18:09:39 字数 984 浏览 1 评论 0原文

我有一个在Struts2中返回LinkedHashMap的函数,我刚刚知道我们不能在struts2中使用for循环,而是必须使用迭代器,并且我是struts新手,

可以帮助我使用迭代器从linkedhashmap中检索值,下面是如何值在 hashmap 中排列:

 LinkedHashMap<String, ArrayList<String>> topSuppliers = new LinkedHashMap<String, ArrayList<String>>();

    while(resultset.next()){

    ArrayList<String> innerList = new ArrayList<String>();

    String manufId = resultset.getString("manufacturer_id");
    String manufLogo = resultset.getString("SUPPLIER_LOGO_IMAGE");
    String manufName = resultset.getString("MANUFACTURER_NAME");
    String manufURL = resultset.getString("MANUFACTURER_URL");

     innerList.add(manufId);
     innerList.add(manufLogo);
     innerList.add(manufName);
     innerList.add(manufURL);

     topSuppliers.put(manufName,innerList);
    }
return topSuppliers;

我想以 4 个制造商为一组显示它们:

Set1:  1,2,3,4
Set2:  5,6,7,8
Set3:  9,10,11,12
etc......

谢谢........

I have function which return LinkedHashMap in Struts2 and i just came to know that we cannot use for loop in struts2 instead we have to use Iterators, and am new to struts

can any on help me to retrieve value from linkedhashmap using iterators, below is how values are lined up in hashmap:

 LinkedHashMap<String, ArrayList<String>> topSuppliers = new LinkedHashMap<String, ArrayList<String>>();

    while(resultset.next()){

    ArrayList<String> innerList = new ArrayList<String>();

    String manufId = resultset.getString("manufacturer_id");
    String manufLogo = resultset.getString("SUPPLIER_LOGO_IMAGE");
    String manufName = resultset.getString("MANUFACTURER_NAME");
    String manufURL = resultset.getString("MANUFACTURER_URL");

     innerList.add(manufId);
     innerList.add(manufLogo);
     innerList.add(manufName);
     innerList.add(manufURL);

     topSuppliers.put(manufName,innerList);
    }
return topSuppliers;

And i want to display them in a set of 4 manufacturers:

Set1:  1,2,3,4
Set2:  5,6,7,8
Set3:  9,10,11,12
etc......

Thank you........

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

岁月流歌 2024-11-17 18:09:39

您应该迭代 List of Map 而不是 Map of List


Example :

@Getter
private List<Map> listOfMap = Lists.newArrayList();

public String execute() {

    while (resultset.next()) {
        final Map<String, String> map = Maps.newHashMap();

        map.put("manufId", resultset.getString("manufacturer_id"));
        map.put("manufLogo", resultset.getString("SUPPLIER_LOGO_IMAGE"));
        map.put("manufName", resultset.getString("MANUFACTURER_NAME"));
        map.put("manufURL", resultset.getString("MANUFACTURER_URL"));

        listOfMap.add(map);
    }

    return SUCCESS;
}

<s:iterator value="listOfMap">
  ${manufId}
  ${manufLogo}
  ${manufName}
  ${manufURL}
</s:iterator>

listOfMap 还可以用作Struts2 JasperReports 插件

You should iterate over List of Map instead of Map of List


Example :

@Getter
private List<Map> listOfMap = Lists.newArrayList();

public String execute() {

    while (resultset.next()) {
        final Map<String, String> map = Maps.newHashMap();

        map.put("manufId", resultset.getString("manufacturer_id"));
        map.put("manufLogo", resultset.getString("SUPPLIER_LOGO_IMAGE"));
        map.put("manufName", resultset.getString("MANUFACTURER_NAME"));
        map.put("manufURL", resultset.getString("MANUFACTURER_URL"));

        listOfMap.add(map);
    }

    return SUCCESS;
}

<s:iterator value="listOfMap">
  ${manufId}
  ${manufLogo}
  ${manufName}
  ${manufURL}
</s:iterator>

The listOfMap also can use as a dataSource for Struts2 JasperReports Plugin.

老娘不死你永远是小三 2024-11-17 18:09:39

您可以在地图上使用 s:iterator 。

<s:iterator value="topSuppliers">
  <s:property value="key" />: <s:iterator value="value" status="status"><s:property /><s:if test="!#status.last">,</s:if></s:iterator>
</s:iterator>

这将使用 Map.Entry 迭代映射,然后使用另一个迭代器和迭代器状态迭代值列表以添加“,”,除非它是最后一个条目。

You can use s:iterator over a map.

<s:iterator value="topSuppliers">
  <s:property value="key" />: <s:iterator value="value" status="status"><s:property /><s:if test="!#status.last">,</s:if></s:iterator>
</s:iterator>

This iterates over the map using Map.Entry and then iterates over your value list using another iterator and iterator status to add "," unless it's the last entry.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文