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.
Related
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>
I'm trying to get the value from a map while iterating through a list. searchResult contains a list of Book objects and each has a list of categories.
categoryFacetCounts is a map that contains counts for each category value. The map has values for all the possible categories (I debugged and checked).
Below is my solution which prints null as the output of the map:
<table class="table table-striped" id="watchlist-table">
<thead>
<tr>
<th>Name</th>
<th>Description</th>
<th>Categories</th>
</tr>
</thead>
<tbody>
<tr th:each="book : ${searchResult}">
<td th:text="${book.name}"></td>
<td th:text="${book.description}"></td>
<td>
<span th:each="c : ${book.categories}">
<span th:text="${c} + '(' + ${categoryFacetCounts[__${c}__]} + ') '"></span>
</span>
</td>
</tr>
</tbody>
</table>
Use the get method of the map, categoryFacetCounts
<span th:each="c : ${book.categories}">
<span th:text="${c} + '(' + ${categoryFacetCounts.get(c)} + ') '"></span>
</span>
Hope this helps.
I am newbie to jquery. In my code I dynamically generate the radio button in jquery.when user clicked on the radio button I should get the siblings value.here is my jsp code.
<div id="result">
<c:forEach var="user" items="${model.userList}">
<table>
<thead>
<tr>
<td>Name</td>
<td>Is Approver</td>
</tr>
</thead>
<tbody>
<c:forEach var="disp" items="${user.value}">
<tr>
<td><c:out
value="${disp.getTblUserDetails().getUserName()}" /></td>
<td class="phase" style="display:none"><c:out value='${user.key.getId()}'/></td>
<td class="users" style="display:none"><c:out value='${disp.getTblUserDetails().getId()}'/></td>
<td><input type="radio" name="approver" class="some" /></td>
</tr>
</c:forEach>
</tbody>
</table>
</c:forEach>
</div>
My Jquery code:
$("#result").on("click",".some",function() {
event.preventDefault();
var phaseid= $(this).siblings('.phase').val();
var userid=$(this).siblings('.users').val();
alert(userid+" "+phaseid);
});
I am getting undefined in alert box.what's wrong with my code.how to I get siblings.
Any help will be greatly appreciated!!!!
.some is the input element inside a td so the .phase and .users are not siblings of this, they are the siblings of the parent of this. So
$("#result").on("click", ".some", function (event) {
event.preventDefault();
var $td = $(this).parent();
var phaseid = $td.siblings('.phase').text();
var userid = $td.siblings('.users').text();
alert(userid + " " + phaseid);
});
Also td does not have value, you may have to use .text()
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>
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.