Tuesday, June 26, 2012

Iterate the List of Map in Jsp using

   We have the list of map in action class. The following sample code show that
how to iterate list of map in jsp file using struts2 iterator. In this, status attribute will give the status of current iteration.

import com.opensymphony.xwork2.ActionSupport;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;



public class mapTest extends ActionSupport {
  public List<Map> listofmap;

  public String execute(){
    listofmap = new ArrayList();
    Map map = new HashMap();
    map.put("a", "America");
    map.put("b", "Brazil");
    map.put("c", "China");
    listmap.add(map);
    Map map2 = new HashMap();
    map2.put("d", "Denmark");
    map2.put("e", "Europe");
    map2.put("f", "France");
    listofmap.add(map2);
    return SUCCESS;
  }
}


<%@taglib prefix="s" uri="/struts-tags"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <body>
        <h1>Map Test</h1>
        <table>
            <thead>
                <tr>
                    <th>key</th>
                    <th>value</th>
                </tr>
            </thead>
            <tbody>
                <s:iterator value="listofmap" status="stat">
                     <s:iterator>
                         <s:property key="key"/>
                        <s:property value="value"/>
                    </s:iterator>
               </s:iterator>
            </tbody>
        </table>
    </body>
</html>

No comments:

Post a Comment