In the below code the DAO class returns a list of objects. Now we need to iterate it and get the values of all the classes separately.
In the debug mode the list of array of object will look like as below.
Now to iterate this kind of list you need to first iterate as usual list and then cast it to Object array. Once its casted then we can get individual POJO classes. Only we need is to cast as per the sequence of the POJO class. Please find the below code.
List mainMenu = daoIntf.getMenu();
logger.debug("List size "+mainMenu.size());
logger.debug("lsit : "+mainMenu.get(0));
Iterator itr=mainMenu.iterator();
while(itr.hasNext()){
Object[] str=(Object[])itr.next();
logger.debug("str.length :"+str.length);
//menu=(Menu)str[0];
subMenuRole=(SubMenuRole)str[0];
subMenu=(SubMenu)str[1];
logger.debug("Menu Name : " +subMenu.getMenu().getName()+ " SubMenu : "+subMenu.getName()+" Role : "+subMenuRole.getRole().getName());
}
We are setting the list in the session as we have got from the DAO class. In this list there are 2 POJO classes so we need to iterate and show it in the front end.
menu.jsp
In the below jsp, please note the <s:iterator> tag is iterating the list which we have set in the session and we have given the list name during iteration is "usermenu". Once we have iterated the list then you can point to the pojo class by using the list name "usermenu[i]" where i is the sequence of pojo class which we have used to iterate the list in the java coding above.
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
</head>
<body>
<div class="arrowlistmenu">
<s:iterator value="%{#session.mainMenuList}" var="usermenu"
status="user">
<s:if test="#user.first == true">
<c:set var="menuElement" scope="page">
<s:property value="#usermenu[0].name" />
</c:set>
<h3 class="menuheader expandable">${menuElement}</h3>
<ul class="categoryitems">
</s:if>
<c:set var="currentMenuElement" scope="page">
<s:property value="#usermenu[0].name" />
</c:set>
<c:choose>
<c:when test="${currentMenuElement==menuElement}">
<li>
<a href="<%=request.getContextPath() %>/<s:property value="#usermenu[2].link" />.html"><s:property value="#usermenu[2].name" /></a>
</li>
</c:when>
<c:otherwise>
</ul>
<c:set var="menuElement" scope="page">
<s:property value="#usermenu[0].name" />
</c:set>
<h3 class="menuheader expandable">${menuElement}</h3>
<ul class="categoryitems">
<c:if test="${currentMenuElement==menuElement}">
<li>
<a href="<%=request.getContextPath() %>/<s:property value="#usermenu[2].link" />.html"><s:property value="#usermenu[2].name" /></a>
</li>
</c:if>
</c:otherwise>
</c:choose>
<s:if test="#user.last == true">
</ul>
</s:if>
</s:iterator>
</div>
</body>
</html>
Hope this code helps some of the developers.
No comments:
Post a Comment