开发者

How to retrieve value from linkedhashmap using iterators in Struts2......?

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 hashma开发者_Go百科p:

 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........


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜