5 Replies Latest reply: Sep 5, 2012 12:01 PM by DrClap RSS

    jsp forEach map

    960113
      Hello



      my english is not super.



      i got a problem (that's why i'm here ;) )


      i got a map => Map<Long, Articles> articleMap

      the key => Long, is the numbers of articles that the client want to buy.
      in Articles, i got getName,getPrice, etc...


      my problem is that i don't know how i can show these informations in a servlet.



      a client can buy different articles. and he can buy 1, 2 or more of the same article (Quantity)
      i need to show in a JPS, all the articles (information => getName, getPrice, get....) that he want to buy with the quantity that he want.



      session.setAttribute ("ArticleMap", articleMap)




      <c:forEach var="article" items="${ArticleMap}">
      ????


      </forEach>


      or if it's not possible what should i do?


      i hope to get a response asp, it's urgent... ;)


      thx
        • 1. Re: jsp forEach map
          ramp
          >
          >
          or if it's not possible what should i do?
          For starters, how about reversing the key and value in that map of yours and see where it takes you ?

          map => Map<Articles, Long> articleMap

          >
          i hope to get a response asp, it's urgent... ;)
          When is it not to the person who is asking?
          • 2. Re: jsp forEach map
            960113
            why should i change it?

            map => Map<Articles, Long> articleMap


            is it possible that u show me with a example how i can get to that map in the jsp to show all the information (Articles => getName, getPrice, etc..) and the Log (amount) with the <c:forEach>
            i don't understand how i can show these informations
            thx
            • 3. Re: jsp forEach map
              ramp
              957110 wrote:
              why should i change it?
              Have you thought how many entries would there be in your map if the user decides to purchase an equal number of different articles?
              map => Map<Articles, Long> articleMap


              is it possible that u show me with a example how i can get to that map in the jsp to show all the information (Articles => getName, getPrice, etc..) and the Log (amount) with the <c:forEach>
              i don't understand how i can show these informations
              thx
              A google search will show you how to use jstl to iterate over a map. I would suggest you try it yourself first and come back here with questions if you are stuck.
              • 4. Re: jsp forEach map
                960113
                i know how to iterate a map


                example:

                for (Map.Entry<Long, Long> map : ArticleMap.entrySet()) {

                               Value = map.getValue());
                               key =map.getKey();
                               
                }


                can i iterate a Map<Long, articlemap> in a jsp?

                i can iterate in a jsp a simple map (<long, long) but in don't know how do iterate (c:foreach) a map<long, othermap>

                Edited by: 957110 on 5-sep-2012 8:15
                • 5. Re: jsp forEach map
                  DrClap
                  Well, like ramp said, you have your keys and values the wrong way around. If your customer orders 2 shirts and 2 hats, what's in your Map?

                  But the real problem is that you didn't do a proper design, and short-cut the process by using a Map. You should have a list of, let's say, Order objects, where an Order contains an Article and a quantity. It's easy to iterate a list.

                  However if you still have more time to spend on doing it wrong, the answer is in the JSTL spec. Section 6.1.2 to be specific.