zoukankan      html  css  js  c++  java
  • How to iterate HashMap using JSTL forEach loop

    JavaServer Tag library is one of the most used JSP tag library out there. I have used it almost in all of my JEE based projects. The best feature probably is the Iterator API in JSTL tag library.

    Here is a small code snippet which you might not know. Its very easy to iterate Lists using JSTL. For example:

     
     Java Code By TONY
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
     
    //Java
    List<String> cityList = new ArrayList<String>();
    cityList.add(
    "Washington DC");
    cityList.add(
    "Delhi");
    cityList.add(
    "Berlin");
    cityList.add(
    "Paris");
    cityList.add(
    "Rome");
     
    request.setAttribute(
    "cityList", cityList);
     
     
    //JSP
    <c:forEach var="city" items="cityList">
        <b> ${city} </b>
    </c:forEach>
    But what if you want to iterate a Hashmap? Well that too is piece of cake. Here is the example:
     
     Java Code By TONY
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
     
    //Java
    Map<StringString> countryCapitalList = new HashMap<StringString>();
    countryCapitalList.put(
    "United States""Washington DC");
    countryCapitalList.put(
    "India""Delhi");
    countryCapitalList.put(
    "Germany""Berlin");
    countryCapitalList.put(
    "France""Paris");
    countryCapitalList.put(
    "Italy""Rome");
             
    request.setAttribute(
    "capitalList", countryCapitalList);
     
    //JSP
    <c:forEach var="country" items="${capitalList}">
        Country: ${country.key}  - Capital: ${country.value}
    </c:forEach>





  • 相关阅读:
    libuv::线程
    libuv::定时器
    libuv::线程池
    libuv::线程同步
    ABAQUS 2017 安装后无法运行问题
    Abaqus2017安装全过程
    ModelCenter安装详解
    centos下安装Ansys 17.2的全部过程
    Jmeter压力测试分布式部署
    Centos7永久挂载iso文件
  • 原文地址:https://www.cnblogs.com/tonycody/p/2994508.html
Copyright © 2011-2022 走看看