Reputation: 95
I want to insert 2 List(orders and prices) into a page(.jsp) and loop 2 list synchronously.I just know insert 1 list into , what is the solution insert 2 LIST into page(jsp) for loop synchronously that, i don't know solution for issue this. THANK ALL.
File:UserController.java
@RequestMapping(value="/{userName}", params = "mada", method=RequestMethod.GET)
public String ordersOfUser(Model uiModel,@PathVariable("userName")String userName){
List<Order> orders = orderService.findAllWithUsername(userName);
List<Float> prices = new ArrayList<Float>();
for(int i=0;i<orders.size();i++){
prices.add(orderService.countPrice(orders.get(i).getId()));
}
uiModel.addAttribute("orders",orderService.findAllWithUsername(userName));
uiModel.addAttribute("prices",prices);
return "orders/orderofuser";
}
File:orderofuser.jsp
<body>
<TABLE>
<c:forEach items="${orders}" var="i">
<tr>
<td><B>ID</B></td>
<td>${i.id}</td>
</tr>
<tr>
<td>Order Date</td>
<TD>${i.orderDate}</TD>
</tr>
<tr>
<td>Number</td>
<td>${i.number}</td>
</tr>
<tr>
<TD>Description</TD>
<td>${i.description}</td>
</tr>
<tr>
<td>Status</td>
<td>${i.status}</td>
</tr>
<tr>
<td>Price</td>
<td>"?"</td>
</tr>
<tr>
<td>-------------------------------------------</td>
<td>-------------------------------------------</td>
</tr>
</c:forEach>
</TABLE>
</body>
Upvotes: 1
Views: 1009
Reputation: 168256
A quick google for "jsp foreach index" and you find the varStatus
attribute which stores an index
and a count
attribute for 0- and 1-based index to where you are in the list.
Just do something like this:
<c:forEach items="${orders}" var="order" varStatus="status">
<tr>
<td><B>ID</B></td>
<td>${order.id}</td>
</tr>
.
.
.
<tr>
<td>Price</td>
<td>${prices.get(status.index)}</td>
</tr>
</c:forEach>
Upvotes: 1
Reputation: 894
You can combine the ORDERS and PRICES in a single bean and then you can iterate the list containing object of new bean.
You can modify your Order bean class and add price as a field in that, and it will look like :
public class Orders {
private String id;
private String orderDate;
private String number;
private String description;
private String status;
private String price;
// with getters and setters
}
In your order service class's findAllWithUsername(userName); method you can apply logic to add price for each order which you are doing in the UserController.
Hope this would help.
Upvotes: 0
Reputation: 4730
If you are getting two seperate lists for your orders and prices on your jsp, then use nested loop as following:
<c:forEach items="${orders}" var="order">
<tr>
<td><B>ID</B></td>
<td>${order.id}</td>
</tr>
.
.
.
<c:forEach items="${prices}" var="price">
<c:if test="${order.id == price.orderId}"> // check if this price is of above order
<tr>
<td>Price</td>
<td>${price.value}</td> // whatever you have properties in your prices list.
</tr>
</c:if>
</c:forEach>
.
.
.
</c:forEach>
Upvotes: 0