I am involved in a JSF project and I am trying to make a view for a shopping cart. a user’s cart can contain tickets which are stored in a hash map (key = id, value = quantity) in the cartBean. In the cart.xhtml I try to output the tickets, here you can see the code:
<h:form>
<c:forEach var="tickets" items="#{cartBean.showTickets()}">
<hr/>
<div class="eight columns">
<div class="two column count">
<h:outputText style="font-size:30px;" value="#{tickets.value}"/>
</div>
<div class="two columns eventimg">
<a href="event.xhtml"><img src="img/dummy/event2.jpg" /></a>
</div>
<div class="six columns eventinfo">
<h4><a href="event.xhtml">Party Hard! </a></h4>
<p>05. Nov 2012</p>
</div>
<div class="two columns price">
<font style="font-size:30px;">35 €</font>
</div>
<div class="eight columns">
<h:outputText value="#{tickets.key}" escape="false"/>
<h:commandButton value="x" action="#{cartBean.removeFromCart(tickets.key)}"/>
</div>
</div>
</c:forEach>
</h:form>
That works fine until I try to delete one ticket from the cart. If I have two different id keys they are displayed correctly in the cart. when I try to delete the ticket with id 1 only the ticket with id 2 remains in the cart which is correct. But when I delete id 2 at first it displays ticket two in the cart although it has been deleted from the hash map. After refreshing the site it displays correctly ticket with id 1, but what is going wrong there?
Please help me!
I have finally found a solution after searching intensively the web. enter link description here here you can see some problems of Problem 1 is simmilar to mine. The solution is to use therefore you have to write the map onto a Arraylist and then iterate the list. here the code how it’s done:
btw. tickets is my hash map in your html file you have to use
<ui:repeat value="#{cartBean.ticketsList}" var="tickets">cartBean is my bean and ticketsList my arrayList. hope you it helps somebody, happy fixing B)