I'm trying to diaplay ArrayList in JSP. I have to ArrayLists I want to loop through and display the data in the same JSP table. This works fine, for the first Arralist (history), but when I get to the second it displays all the ArrayList in each row, instead of the current index from the loop:
<table id="test" class="table text">
<tr>
<th>Result</th>
<th>Deposit</th>
<th>Bet</th>
<th>Return +/-</th>
<th>Current balance</th>
<th>Date</th>
</tr>
<%
for (history his : history) {
%>
<tr class="listing">
<td><%=his.getRes()%></td>
<td><%=resultTwoDecimalsDeposit%></td>
<td><%=his.getOdds()%></td>
<td><%=his.getDate() %> </td>
<td style="color:#00cc00;"> + <%=resultTwoDecimalsRet%> </td>
Everything is fine until here. Instead of showing the current index from the loop, it displays all the ArrayList in each tr
<% for (int i = 0; i < list1.size(); i++) {
%>
<td><%=list1.get(i) %></td>
<% } %>
<%}}}%>
</tr>
</table>
Your problem is because of the nested loop. For each his, the complete inner for loop is executing which you do not want. You want that for each history element, the corresponding value from list1 is displayed.
Do it as follows:
<table id="test" class="table text">
<tr>
<th>Result</th>
<th>Deposit</th>
<th>Bet</th>
<th>Return +/-</th>
<th>Current balance</th>
<th>Date</th>
</tr>
<%for (int i=0; i<history.size() && i<list1.size(); i++) {%>
<tr class="listing">
<td><%=history.get(i).getRes()%></td>
<td><%=resultTwoDecimalsDeposit%></td>
<td><%=history.get(i).getOdds()%></td>
<td><%=history.get(i).getDate() %> </td>
<td style="color:#00cc00;"> + <%=resultTwoDecimalsRet%> </td>
<td><%=list1.get(i) %></td>
</tr>
<%}%>
</table>
Related
I am retrieving answer from ans column by using jdbc and showing it into a jsp page in a form of a table, if ans column is null or empty then I have to give a user edit link, but if ans column has a value then I'm going to show answer but the problem is if-else condition is not working. Please help me to solve this, My code is given below:
<div class="table-responsive">
<table class="table center-aligned-table">
<thead>
<tr class="text-primary">
<th>Question No</th>
<th>Question Name</th>
<th>Answer:</th>
<th> </th>
<th> </th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<%
try{
int oopa=1;
//String nme=(String)session.getAttribute("vname7");
DbCon cc=new DbCon();
Connection onn=cc.fun();
Statement stt=onn.createStatement();
ResultSet r=stt.executeQuery("select ques,ans from postquestion;");
while(r.next()){
%>
<tr class="">
<td><center><%=oopa++%></center></td>
<td><%=r.getString(1)%></td>
<%
if(r.getString(2)==null)
{
%>
<td>Edit</td>
<%
}
else
{
%>
<td><%=r.getString(2)%></td>
<%
}
%>
<td> </td>
</tr>
<%
}
}
catch(Exception vjin){
System.out.println("I am vjin: "+vjin);
vjin.printStackTrace();
}
%>
</tbody>
</table>
</div>
You might get empty values from your results. So check if r.getString(2) is empty or not, null differs from empty value.
More over, JSP is just a view component, writing DAO code in JSP is not a good practice.
Thanks
You can check for null values using wasNull() method of ResultSet.For example like below :
<%
//getting value of column in "a"
String a = r.getString(2);
//checking if resultset return null i.e a is null then do below:
if(r.wasNull())
{
%>
<td>Edit</td>
<%
}
else
{
%>
<td><%=r.getString(2)%></td>
<%
}
%>
I have a form with some inputs; each input returns a list of data which is displayed in a table in another html page. Each input have a table to display it's data. My task is to do not display the data if the input is not entered by the user.
Here is my code
<!-- Country Table-->
<%for(int i = 0; i < countryList.length;i++){
if(countryList.length == 0)
break;
%>
<div class="box" align="center">
<table name="tab" align="center" class="gridtable">
<thead >
<tr>
<th style="width: 50%" scope="col">Entity Watch List Key</th>
<th style="width: 50%" scope="col">Watch List Name</th>
</tr>
</thead>
<tbody>
<tr>
<td style="width: 50%"><%out.println((String) (countryList[i].getEntityWatchListKey()));%></td>
<td style="width: 50%"><%out.println((String) (countryList[i].getEntityName()));%></td>
</tr>
</tbody>
</table>
</div>
<%}%>
I am using break to go out of the loop to do not display the table, is that true ?
You can use this condition before the for loop,
if(countryList.length != 0)
or
if(countryList.length > 0)
and then you need not use the break condition,
Furthermore the for loop you have currently defined will not work because if the length of the array is 0 then this condition i < countryList.length will become 0<0 and it will fail,so your for loop won't even be entered.So your current if condition if(countryList.length == 0) will not be accessed.
Please modify your code
<div class="box" align="center">
<table name="tab" align="center" class="gridtable">
<thead >
<tr>
<th style="width: 50%" scope="col">Entity Watch List Key</th>
<th style="width: 50%" scope="col">Watch List Name</th>
</tr>
</thead>
<tbody>
<%for(int i = 0; i < countryList.length;i++){
if(countryList.length > 0) %>
<tr>
<td style="width: 50%"><%out.println((String) (countryList[i].getEntityWatchListKey()));%></td>
<td style="width: 50%"><%out.println((String) (countryList[i].getEntityName()));%></td>
</tr>
<%}%>
</tbody>
</table>
</div>
For a good practice you have to repeat the row not the table.
I have two arrays:
<%String TXTfileArray[] = (String[])request.getAttribute("txt");%>
<%String TXTContentArray[] = (String[])request.getAttribute("content");%>
the data in the array is repeated.
by using the following statement in JSP i can output the array in the table:
<table border="1">
<tr>
<th>txt name</th>
<th>txt content</th>
</tr>
<% for (int i=0; i< TXTfileArray.length;i++){ %>
<tr>
<td> <%=TXTfileArray[i] %> </td>
<td> <%=TXTContentArray[i] %> </td> <%} %>
</tr>
</table>
how to remove the repeated element in the table?
Please do not hard code, as the array is not fixed. the array is depended on the other process.
You could try adding each element in a Set, and if it already exists don't add it to the table:
<% Set<String> a = new HashSet<>();
for (int i=0; i< TXTfileArray.length;i++)
if (a.add(TXTfileArray[i]) { %>
<tr>
<td> <%=TXTfileArray[i] %> </td>
<td> <%=TXTContentArray[i] %> </td> <%} %>
Although I think JB Nizet's suggestion of using LinkedHashSet<SomeClass> is probably a better way to do it.
I am building a Spring MVC web app, I have an object called NodeRel which is defined as below:
public class NodeRel {
private String fromNodeId;
private String toNodeId;
private String fromNodeName;
private String toNodeName;
private List<QuotaValueOnTime> fromNodeSend;
private List<QuotaValueOnTime> toNodeSend;
//getters and setters omitted
}
In the server side code, i obtained a list of NodeRels and bind it to the model. In the jsp page, I want to first loop through the List and then inside it, I want to loop though List. My jsp code:
<div class="table-responsive">
<table class="table table-striped table-bordered table-hover">
<thead>
<tr>
<th class="center">Count</th>
<th>relation</th>
<th colspan='25'>Detail</th>
</tr>
</thead>
<tbody>
<c:forEach var="nodeRel" items="${nodeRelInfo}" varStatus="stc">
<tr>
<td rowspan="3">${stc.count}</td>
<td rowspan="3">${nodeRel.fromNodeName} --> ${nodeRel.toNodeName}</td>
<td>\</td>
<c:forEach var="x" begin="0" end="23" step="1">
<td>${x}</td>
</c:forEach>
</tr>
<tr>
<td>Send_A</td>
<c:forEach var="node" items="${nodeRelInfo.fromNodeSend}">
<td>${node.sumval}</td>
</c:forEach>
</tr>
<tr>
<td>Send_B</td>
<c:forEach var="x" begin="0" end="23" step="1">
<td>${x}</td>
</c:forEach>
</tr>
</c:forEach>
</tbody>
</table>
</div>
My code doesn't work and I got java.lang.NumberFormatException: For input string: "fromNodeSend" near the second loop:
<c:forEach var="node" items="${nodeRelInfo.fromNodeSend}">
<td>${node.sumval}</td>
</c:forEach>
What is wrong with my code?
Note that the variable ${nodeRelInfo} represents the List and the variable ${nodeRel} represents the each item you work with.
Thus the item you want to loop in the second loop is ${nodeRelInfo.fromNodeSend}. Change the second name of variable looped:
<c:forEach var="node" items="${nodeRel.fromNodeSend}">
<td>${node.sumval}</td>
</c:forEach>
It works on the same logic like Java for-each loop.
for (List nodeRel: nodeRelInfo) {
// bla blaa
for (String node: nodeRel.fromNodeSend()) {
System.out.println(node);
}
}
change your second loop like this because your variable name in parent loop is nodeRel not nodeRelInfo
<c:forEach var="node" items="${nodeRel.fromNodeSend}">
<td>${node.sumval}</td>
</c:forEach>
Please visit the website "http://www.cricbuzz.com/cricket-series/2223/icc-cricket-world-cup-2015/points-table"
<table class="table cb-srs-pnts">
<thead>
<tr class="cb-srs-gray-strip">
<th class="cb-col-20 cb-srs-pnts-th text-left" style="padding-left: 6px;">Pool B</th>
<td class="cb-srs-pnts-th">Mat</td>
<td class="cb-srs-pnts-th">Won</td>
<td class="cb-srs-pnts-th">Lost</td>
<td class="cb-srs-pnts-th">Tied</td>
<td class="cb-srs-pnts-th">NR</td>
<th class=" cb-srs-pnts-th">Pts</th>
<td class="cb-srs-pnts-th">NRR</td>
<th/>
</tr>
</thead>
<tbody>
</table>
I have to print all the table headings present for first table. It has a combination of "td" and "th" tags.
I am using the following xpath to retrive them.
//h3/../table[1]/thead/tr/*[self::td or self::th]
All the values are getting printed except for the text "Pool B"
Can somebody tell me why "Pool B" text is not getting selected?
Code to print the output:
driver.get("cricbuzz.com/cricket-series/2223/icc-cricket-world-cup-2015/…);
System.out.println(driver.findElement(By.xpath(" //h3/../table[1]/thead/tr/th")).getText());
List<WebElement> tableHeading = driver.findElements(By .xpath("//h3/../table[1]/thead/tr/*[self: : tdorself: : th]"));
for (int i = 1; i < tableHeading.size(); i++)
{
System.out.println(i+""+tableHeading.get(i).getText());
}
Set the index to 0 in your for loop:
for (int i = 0; i < tableHeading.size(); i++)
{
System.out.println(i+""+tableHeading.get(i).getText());
}