jstl loop through a list in an object - java

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>

Related

Populating inner table inside another table with Thymeleaf

I have DTO class:
private LocalDate date;
private List<String> firstShift;
private List<String> secondShift;
private List<String> thirdShift;
With getters, setters and toString for every field.
I'm creating this table for shift schedule calendar:
My Thymeleaf:
<table class = "table table-striped table-hover" id = "schedule_table">
<thead>
<tr>
<th class = "th_schedule_table">Date</th>
<th class = "th_schedule_table">First Shift</th>
<th class = "th_schedule_table">Second Shift</th>
<th class = "th_schedule_table">Third Shift</th>
</tr>
</thead>
<tbody>
<tr th:each = "calendar_node : ${calendarNodeList}">
<td th:text = "${calendar_node.date}"></td>
<td>
<table>
<tbody>
<tr th:each = "employee, i : ${firstShift}">
<td th:text = "${firstShift.[i]}"></td>
</tr>
</tbody>
</table>
</td>
<td>
<table>
<tbody>
<tr th:each = "employee: ${secondShift}">
<td th:text = "${employee}"></td>
</tr>
</tbody>
</table>
</td>
<td>C</td>
</tr>
</tbody>
</table>
The idea behind this: CalendarNode.date generates row for parent table, for each date. That works fine.
Inside every row, in a cell I have second table that should show list of employees who works on that date in that shift. If I have calendar_node object in one row, I'm using his "firstShift" field to generated rows for second inner table.
The problem is that I'm getting empty table. I checked my back-end and I have two employees for first date (18th July), first shift, one employee for second shift, but none is shown. I tried a lot of different syntax for Thymeleaf, none of it works. I guess I did Thymeleaf wrong?
UPDATE:
Example of data that has been passed to web page via model object:
If firstShift is a field of calendar_node, then you need to actually address it as such in the template:
${calendar_node.firstShift} instead of ${firstShift}

Loop inside loop - Display ArrayList

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>

How to display recursive data on jsp?

List<TextualReq> textualReqList = session.createQuery("from TextualReq where parent is null").list();
for(TextualReq root : textualReqList){
logger.info("textualReqList ::"+root.getId());
logger.info("textualReqList ::"+root.getData());
logger.info("textualReqList ::"+root.getParent());
logger.info("textualReqList ::"+root.getChildren());
root.display( " " );
}
public void display( String margin )
{
System.out.println( "================="+margin + data );
for ( TextualReq child : children )
{
child.display( margin + " " );
}
}
In Java This function will print the data in below format. I will like to display this data in jsp page. can some one help me how to do I post this data to UI and display recursively. here root.getChildren() is again a private Set children = new HashSet();
MyRootData
MyChild1Data
MyGrandchild11Data
MyGrandchild12Data
MyGreatGrandchild121Data
MyGrandchild13Data
MyChild2Data
MyGrandchild21Data
MyGrandchild22Data
MyRootData
MyChild1Data
MyGrandchild11Data
MyGrandchild12Data
MyGreatGrandchild121Data
MyGrandchild13Data
MyChild2Data
MyGrandchild21Data
MyGrandchild22Data
Jsp code that I have is
<h3>TextualReq List</h3>
<c:if test="${!empty listtextualReq}">
<table class="tg">
<tr>
<th width="80">textualReq ID</th>
<!-- <th width="120">textualReq parent</th> -->
<!-- <th width="120">textualReq children</th> -->
<th width="120">textualReq data</th>
<th width="60">Edit</th>
<th width="60">Delete</th>
</tr>
<c:forEach items="${listtextualReq}" var="textualReq">
<tr>
<td>${textualReq.id}</td>
<%-- <td>${textualReq.parent}</td> --%>
<%-- <td>${textualReq.children}</td> --%>
<td>${textualReq.data}</td>
<td><a href="<c:url value='/edittextualReq/${textualReq.id}' />" >Edit</a></td>
<td><a href="<c:url value='/removetextualReq/${textualReq.id}' />" >Delete</a></td>
</tr>
</c:forEach>
</table>
</c:if>
Now it is displaying like
Rather than displaying the data with each recurse, build a String. I would pass a StringBuilder into the recursive function, then write a getStuff() method that calls that function, and returns the string. The JSP would simply refer to the getStuff() method.

Working with <div> tag in JSP for making the contents hidden or visible

I have written a javascript function.
function wellChecked(state) {
if (state)
{
wellDropDown.style.visibility = 'visible';
}
else
{
wellDropDown.style.visibility = 'hidden';
}
}
I have a checkbox after the Well Modification <td> as given below,
<tr>
<td>On Call</td>
<td><html:checkbox property="onCall"/></td>
<td>Well Modification</td>
<td><input type="checkbox" onclick="wellChecked(this.checked)" /></td>
</tr>
When that checkbox is clicked I want the drop down list given under the div id=wellDropDown to be displayed. Defaultly, if the check box is not clicked, the drop down should not be displayed.
<tr>
<td>Active</td>
<td><html:checkbox property="active"/></td>
<div id="wellDropDown" style="visibility:hidden;">
<td>
<html:select property="wellFormatId">
<html:option value="">(Select)</html:option>
<bean:define id="wellFormatColl" name="wellFormats" scope="request"/>
<logic:iterate id="wellFormats" name="wellFormatColl" indexId="index" type="com.astrazeneca.compis.data.WellFormatVO">
<% Long wellId = wellFormats.getWellFormatId();%>
<% String wellIdNo = wellId.toString(); %>
<html:option value="<%=wellIdNo%>">
<bean:write name="wellFormats" property="wellFormatName"/>
</html:option>
</logic:iterate>
</html:select>
</td>
</div>
</tr>
When I tried executing this code, I could see the drop down list getting displayed irrespective of the checkbox checked or not.
Where I have went wrong in this scenario? Please give ur suggestions or ways to implement my requirement.
Your HTML is invalid. You may not have a div enclose a td like this. Either make the td itself visible or invisible, or put the div inside the td, instead of putting it around the td.
Also, unless wellDropDown is a global JS variable, the code should be
document.getElementById("wellDropDown").style.visibility = 'visible';
with jquery you could do this :
<tr>
<td>On Call</td>
<td><html:checkbox property="onCall"/></td>
<td>Well Modification</td>
<td><input type="checkbox" id="myCheckBox" /></td>
</tr>
...
<script>
$('#myDropDown').click(
function () {
$("#wellDropDown").toggle();
});
);
</script>

how to display data obtained from dao in jsp

in jsp
<table width="100%" id='table1' border="0" cellspacing="2" cellpadding="2">
<tr class="tab-highlighted-2">
<td class="tab-highlighted-2" width="10%">
<div align="left">Project ID</div>
</td>
<td class="tab-highlighted-2" width="10%">
<div align="left">Project Name</div>
</td>
<td class="tab-highlighted-2" width="20%">
<div align="left">Cost Center</div>
</td>
<td class="tab-highlighted-2" width="20%">
<div align="left">Manager</div>
</td>
</tr>
<tr class="bg-row1">
<c:forEach items="${resultList}" var="resultList">
<td class="td-highlighted-2">
<div align="left">${resultList.Projid}</div>
</td>
<td class="td-highlighted-2">
<div align="left">${resultList.Projname}</div>
</td>
<td class="td-highlighted-2">
<div align="left">${resultList.Cost}</div>
</td>
<td class="td-highlighted-2">
<div align="left">${resultList.Manager}</div>
</td>
</tr>
</table>
in dao
public final class SearchProjDAO
{
private static InitialContext context;
String CLASS_NAME="DBConnectionFactory";
public ArrayList submitProjectDetails(SearchProjVO searchprojVO)
{
ArrayList ar = new ArrayList();
String methodname="createConnection";
Connection conn = null;
PreparedStatement psmt;
try {
System.out.println("in DAO");
System.out.println("searchprojVO id=="+searchprojVO.getProjid());
conn = DBConnection.getJNDIConnection();
ResultSet rs = null;
String query="select * from CR_EMPLOYEE_DETAILS";if(searchprojVO.getProjid()!=null || searchprojVO.getProjname()!=null || searchprojVO.getManager()!=null)
query=query+" where ";
if(searchprojVO.getProjid()!=null)
query=query+" PROJ_ID="+searchprojVO.getProjid();
if(searchprojVO.getProjname()!=null)
query=query+"PROJ_NAME="+searchprojVO.getProjname();
if(searchprojVO.getCost()!=null);
query=query+"PROJ_COST="+searchprojVO.getCost();
if(searchprojVO.getManager()!=null)
query=query+"PROJ_MANAGER="+searchprojVO.getManager();
psmt= conn.prepareStatement(query);
rs=psmt.executeQuery();
while(rs.next())
{
SearchProjVO projVO = new SearchProjVO();
projVO.setProjid(rs.getString("PROJ_ID"));
projVO.setManager(rs.getString("PROJ_NAME"));
projVO.setProjname(rs.getString("PROJ_COST"));
projVO.setManager(rs.getString("PROJ_MANAGER"));
ar.add(projVO);
}
System.out.println("conn==="+conn);
} catch (Exception e) {
e.printStackTrace(System.err);
}
return ar;
}
}
I spot several mistakes:
Here,
<c:forEach items="${resultList}" var="resultList">
You're overriding the list value with the value of the list item everytime. Don't do that. Give the var an unique variable name. The entity name is the most straightforward choice.
<c:forEach items="${resultList}" var="project">
Note that I'd personally also rename the nothing-saying resultList to a more self-explaining projects.
And here,
<tr class="bg-row1">
<c:forEach items="${resultList}" var="project">
the flow is wrong. You should print a new row inside each loop. Swap them.
<c:forEach items="${resultList}" var="project">
<tr class="bg-row1">
And here,
${resultList.Projid}
${resultList.Projname}
${resultList.Cost}
${resultList.Manager}
the property names must start with lowercase (and fix the item name to be the same as in var).
${project.projid}
${project.projname}
${project.cost}
${project.manager}
Note that I'd personally also get rid of proj prefix in some property names.
And finally you're forgotten a closing </c:forEach>.
</tr>
</c:forEach>
Unrelated to the concrete problem, your JDBC code is sensitive to SQL injection attacks and is leaking resources. Fix it as well.

Categories

Resources