I am using JSTL for each loop for iteration over a list & display that data in a table.
I would like to know how to display the serial number in that table.
<c:forEach items="${item.getApps()}" var="app">
<tr><td>....</td> //need to display serial number here
<td><c:out value="${app.getApp()}"></c:out></td>
</tr></c:forEach>
TIA.
You should use varStatus attribute of that tag which keep track of loop status
<c:forEach items="${item.getApps()}" var="app" varStatus="counter">
<tr><td> ${counter.count}"</td> //need to display serial number here
<td><c:out value="${app.getApp()}"></c:out></td>
</tr></c:forEach>
This should work for you.
Try this, Using varStatus property
<c:forEach items="${item.getApps()}" var="app" varStatus="loop">
<tr><td>${loop.index}</td> //need to display serial number here
<td><c:out value="${app.getApp()}"></c:out></td>
</tr></c:forEach>
This should be work definitly
<c:forEach items="${item.getApps()}" var="app" varStatus="counter">
<tr><td> ${counter.index+1}"</td>
<td><c:out value="${app.getApp()}"></c:out></td>
</tr>
</c:forEach>
Related
I want to give my users an ability to remove some items from database. Like this:
I have an array in jsp file:
<c:forEach items="${page.content}" var="row" varStatus="status">
<tr>
<td><input type="checkbox"/></td>
<td>${row.name}</td>
<td>${row.profession}</td>
<td>${row.phone}</td>
<td>${row.city.name}</td>
<td><img src="img/del.png"/></td>
Is there any way to remove an item from array "row" (for example, when I click on the picture del.png)? After that, I will send this array to the controller, to let it know which items I want to remove.
So write a if condition inside foreach tag like this.My idea is to exclude or skip the creation of html if if condition is not satisfied with the value you will specify.
<c:forEach items="${page.content}" var="row" varStatus="status">
<c:if test="${row.value != '<What you want to exclude>'}">
<tr>
<td><input type="checkbox"/></td>
<td>${row.name}</td>
<td>${row.phone}</td>
<td>${row.city.name}</td>
</tr>
</c:if>
</c:forEach>
I have a picture attached with pretty much all the information. To add to it. The table data is stored within a java bean within an ArrayList (Each record is one bean).
I would like my application to be able to recognize that I am in CYCLE_ID n and have all the runs displayed in a table (see Desired Results), and for each CYCLE_ID the same thing.
I am open to making changes in the background java class (not shown), or within the jsp.
<div id="activeQueue">
<h2>Active Modeling Queue for <bean:write name="userBean" property="uid" /> </h2>
<c:forEach items="${activeQueueList}" var="currentQueue">
<p>Queue {<c:out value="${currentQueue.modelQueueID}" />} - <c:out value="${currentQueue.PROJECTION_PROJECT_NME}" /></p>
<table>
<tr>
<th>Type</th>
<th>Start Time </th>
<th>Status</th>
</tr>
<tr>
<td><c:out value="${currentQueue.modelQueueDes}" /></td>
<td><c:out value="${currentQueue.ROW_CREATE_TSP}" /></td>
<td><c:out value="${currentQueue.STATUS_DES}" /></td>
</tr>
</table>
</c:forEach>
First prepare a Map using CYCLE_ID as key and the Queue List as the value. Assuming the bean class is ActiveQueue you can do the following
Map<Integer, ActiveQueue> data = new HashMap<Integer, ActiveQueue>();
for(ActiveQueue queue : activeQueueList) {
if(!data.containsKey(queue.CYCLE_ID)) {
data.put(queue.CYCLE_ID, new ArrayList<ActiveQueue>);
}
data.get(queue.CYCLE_ID).add(queue);
}
Now you can use two nested loops in jsp. The outer loop iterates over the Map data and the inner loop iterates over the corresponding Map.entry values. Like following
<div id="activeQueue">
<h2>Active Modeling Queue for <bean:write name="userBean" property="uid" /></h2>
<c:forEach items="${data}" var="entry">
<p>Queue {<c:out value="${entry.key}" />} - <c:out value="${entry.value[0].PROJECTION_PROJECT_NME}" /></p>
<table>
<tr>
<th>Type</th>
<th>Start Time </th>
<th>Status</th>
</tr>
<c:forEach items=${entry.value} var="currentQueue">
<tr>
<td><c:out value="${currentQueue.modelQueueDes}" /></td>
<td><c:out value="${currentQueue.ROW_CREATE_TSP}" /></td>
<td><c:out value="${currentQueue.STATUS_DES}" /></td>
</tr>
</c:forEach>
</table>
</c:forEach>
Instead of having a List<Run> and iterating on it, you should have a List<Cycle>, where each Cycle contains a List<Run>.
Grouping the original flat list of Runs by Cycle is easy with a Map storing the ID of the cycles as keys, and the corresponding cycle as value:
you iterate over the runs
for each run, you fetch the corresponding cycle by ID in the map. If it's not there yet, you create it and store it in the map
you add the run in the cycle
Ath the end, the keySet() of the map contains all the cycles, and each cycle contains all its runs.
Then use two nested foreach loops in the JSP.
I am working on servlet and jsp project. I am passing an object from servlet to JSP. And currently I am iterating that object and showing them in a table -
Below is my code in jsp -
<TABLE id="tableSMS" BORDER="1" CELLPADDING="3" CELLSPACING="1" style="text-align: center;">
<TR style="color:#ffffff;background-color:#787878;">
<TH>Hash Name</TH>
<TH>Database Name</TH>
<TH>Version</TH>
</TR>
<c:forEach var="i" begin="0" end="${reportCount.getHash().size() - 1}">
<TR>
<TD>
${reportCount.getHash().get(i)}
</TD>
<TD>
${reportCount.getDatabaseName().get(i)}
</TD>
<TD>
${reportCount.getVersion().get(i)}
</TD>
</TR>
</c:forEach>
And above code is working fine and I am able to show the data properly. Now what I need to do is -
${reportCount.getDatabaseName().get(i)} will return database name as oracle or mysql only. Now I need to calculate what is the percentage of oracle database. I will get the total number of records from ${reportCount.getHash().size(). So if total number of records is 10 and oracle database is present 5 times, then the percentage should be 50%.
Now I am not sure how would I calculate the above percentage using that object? And after calculating that percentage, I need to show the result in a new table which is shown below -
<table>
<tr>
<th style="background-color: #E8E8E6;"><b>Oracle Database</b></th>
</tr>
<tr>
<!-- I would like to show the percentage in this row -->
<td>%</td>
</tr>
</table>
I am thinking I should iterate the reportCount object again in the above new table and extract the percentage here but not sure how would I do that? Can anyone provide an example?
UPDATE:-
Here is my bean code -
public class Response {
private List<String> hash = new LinkedList<String>();
private List<String> databaseName = new LinkedList<String>();
private List<String> version = new LinkedList<String>();
// getters and setters here
}
There are three things as following:
Add the functions taglib:
<%# taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
Add reportsCounts variable to requestScope instead of session if not required on any other page & use as following:
<c:set var="oracleDBCount" value="0" scope="page" />
<c:forEach var="reportCount"
items="${requestScope.reportCounts.databaseName}" varStatus="loop">
<TR>
<TD>${requestScope.reportCounts.hash[loop.index]}</TD>
<TD>${reportCount}
<c:if test="${reportCount == 'ORACLE'}">
<c:set var="oracleDBCount" value="${oracleDBCount + 1}" scope="page"/>
</c:if>
</TD>
<TD>${requestScope.reportCounts.version[loop.index]}</TD>
</TR>
</c:forEach>
Now display percentage as:
${(oracleDBCount / fn:length(requestScope.reportCounts))*100}%
Based on my experience, doing number-crunching on a JSP page can make it unreadable very quickly. Also you may encounter additional requirements in the future such as:
Text matching (Are you guaranteed that the value is always "oracle"? Or can it become "ORACLE" or "Oracle"?
What if there are zero reports? Then you will need an if-condition to prevent division by zero.
If your client says "We have more report servers like MS, Postgres...can you show the percentage of those?"
Is it possible to do the computation inside the servlet while you are making the reportCount object?
Then you can pass the value inside a session attribute
request.getSession(true).setAttribute("oracleReports", "50%")
and then in the JSP output something like
<c:out value="${sessionScope.oracleReports}"/>
Use JavaScript for doing the operation, Here is the code
Read the comments to understand the code.
Add an id to the so that i can read the inner content using
javascript. Appended id with the index so that each raw gets different
id's
<c:forEach var="i" begin="0" end="${reportCount.getHash().size() - 1}">
<TD id="databaseName<c:out value='${i}' />">
${reportCount.getDatabaseName().get(i)}
</TD>
Add Id to the so that i can set values to the raw using
JavaScript
<tr>
<td id="oraclePercentage">%</td>
<td id="mySQLPercentage">%</td>
</tr>
call the javascript function to set the values to the raw, as we don't
have any button to trigger we open a raw and add the JavaScript call
from the JSP so that the script is triggered every time the page loads
the raw.
<TR><TD><Script>setPercentage('${reportCount.getHash().size() - 1}');</script><TD><TR>
what to do is defined here
<Script>
function setPercentage(var index){
for(var i=0; i<index;i++){
var documentType=document.getElementById("databaseName"+i).innerHTML;
var oracleCount=0;
var mySQLCount=0;
if(vardocumentType=='Oracle')
oracleCount+=(Number)1;
else if(vardocumentType=='MySQL')
mySQLCount+=(Number)1;
document.getElementById("oraclePercentage").innerHTML=(oracleCount/index)*100;
document.getElementById("mySQLPercentage").innerHTML=(mySQLCount/index)*100;
}
</script>
i have to display 3 array lists in a table.. the arraylists are forwarded from a servlet. the value s coming to the jsp. as if we display one array list alone the prg is woking but to display 3 array lists is a problem.
<c:forEach var="auctionDO" items="${auctionDOListServlet}">
<tr div class="odd">
<td style="font-size:14px;"><c:out value="${auctionDO.auctionName}"/> </td>
<td><c:out value="${auctionDO.endDate}"/> </td>
<td><c:out value="${auctionDO.status}"/></td>
<td><c:out value="${depotDo.depotName}"/></td>
<td><c:out value="${userAuctionRelDo.bidAmt}"/></td>
</tr>
</c:forEach>
as auctionDOListServlet is provided in foreach its displaying correctly.. the depotDO and userAuctionRelDO is not being displayed..
how do i display the other 2 arraylists??
the arraylists are passed from servlet as follows
request.setAttribute("auctionDOListServlet", auctionDOListServlet);
request.setAttribute("depotDOListServlet", depotDOListServlet);
request.setAttribute("userAuctionRelDOListServlet", userAuctionRelDOListServlet);
i have combined the three list into 1 list
megaList.addAll(auctionDOListServlet);
megaList.addAll(depotDOListServlet);
megaList.addAll(userAuctionRelDOListServlet);
now how do i display this in for each?? would this work?
where mega is the name of the variable??
<td><c:out value="${mega.endDate}"/> </td>
this now hows errors for depot name and bidamt as they are in a totally different DO.. wat to do??
how do i display the other 2 arraylists??
As you did. depotDOListServlet and userAuctionRelDOListServlet are two array lists then same way you can use <c:forEach></c:forEach> to iterate those lists.
<c:forEach var="auctionDO" items="${auctionDOListServlet}">
<tr div class="odd">
<td style="font-size:14px;"><c:out value="${auctionDO.auctionName}"/> </td>
<td><c:out value="${auctionDO.endDate}"/> </td>
<td><c:out value="${auctionDO.status}"/></td>
<c:forEach var="depotDo" items="${depotDOListServlet}">
<td><c:out value="${depotDo.depotName}"/></td>
</c:forEach>
<c:forEach var="userAuctionRelDo" items="${userAuctionRelDOListServlet}">
<td><c:out value="${userAuctionRelDo.bidAmt}"/></td>
</c:forEach>
</tr>
</c:forEach>
Remember that for every item in list auctionDOListServlet rest two lists will iterate each time.
You are iterating over one particular array. This has no effect on other arrays. The other arrays are not being iterated over.
I would create a container object that holds these three entities together, and then put these in a list.
Or you could for a for loop and call getValueAt(anIntValue) on the list. This way you could get the correct value for every list.
In my java web application, I have a data table called "sell"
sell(id_sell,id_buyer,id_product,date,final_price,shipping_price,sales_tax)
I have created a jsp page which display some information about the user, such as the products which won at the auction; these products are described by "sell". In this page I have to use jstl library and my idea is to use 'c:forEach' tag to iterate every row of the table.
Here is my code:
<table class="table table-bordered">
<thead>
<tr>
<th>Product</th>
<th>Date</th>
<th>Final Price</th>
<th>Shipping Price</th>
<th>Salex Tax</th>
</tr>
</thead>
<tbody>
<c:forEach var="s" items="${sell}">
<tr>
<td><c:out value="${s.id_product}"/></td>
<td><c:out value="${s.date}"/></td>
<td><c:out value="${s.final_price}"/></td>
<td><c:out value="${s.shipping_price}"/></td>
<td><c:out value="${s.sales_tax}"/></td>
</tr>
</c:forEach>
</tbody>
</table>
But this code shows only an empty table. Where am I doing wrong? Do I have to import something?
From your servlet, load the needed data from your data source, and add it to a scope. Your JSP will then be able to access it through an EL expression. For example, to add data in the request scope:
List<> data = yourDao.list();
request.setAttribute("sell", data);
About your comment on your own question, use the session scope wisely. Good practice is to use the smallest scope possible.