troubleshooting Question

JSP fn:length function not working on LinkedHashMap

Avatar of dolphin_g
dolphin_g asked on
Java EEJSP
3 Comments1 Solution8051 ViewsLast Modified:
Hi,

I'm trying to find the length of a Map by using the fn:length function in JSP. I've used it before, and I'm sure it works. But in the below case, It throws an Exception:

2009-03-17 16:23:59,679 ERROR [org.apache.struts.taglib.tiles.InsertTag] ServletException in '/wealth/portfolio.jsp': Problems calling function 'fn:length'
org.apache.jasper.JasperException: An exception occurred processing JSP page /wealth/portfolio.jsp at line 345

342:                                     </c:if>
343:                               </pg:item>      
344:                               </c:forEach>
345:                               ${fn:length(taxLotSecurityClassMap)}
346:                               <c:if test="${fn:length(taxLotSecurityClassMap) > 1}">
347:                                     <tr>
348:                                           <td align="left" colspan="5" nowrap>&nbsp;</td>


Stacktrace:
      at org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:518)
      at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:429)
      ...............
org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:580)
      at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
      at java.lang.Thread.run(Thread.java:595)
Caused by: javax.el.ELException: Problems calling function 'fn:length'
      at org.apache.el.parser.AstFunction.getValue(AstFunction.java:99)
      at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:186)
      at org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(PageContextImpl.java:928)
      at org.apache.jsp.wealth.portfolio_jsp._jspService(portfolio_jsp.java:1041)
      at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
      at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:387)
      ... 59 more
Caused by: javax.servlet.jsp.JspTagException: Don't know how to iterate over supplied "items" in &lt;forEach&gt;
      at org.apache.taglibs.standard.functions.Functions.length(Functions.java:228)
      at sun.reflect.GeneratedMethodAccessor157.invoke(Unknown Source)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      at java.lang.reflect.Method.invoke(Method.java:585)
      at org.apache.el.parser.AstFunction.getValue(AstFunction.java:94)
      ... 65 more

Below attached is the code snippet in JSP. The data structures are

taxLotDetailsMap  - LinkedHashMap<String, LinkedHashMap<String,ArrayList<someObject>>>;

Ive tried to specify in the logic:iterate tag the "type" attribute as 'java.util.LinkedHashMap', it throws an Error:

2009-03-17 16:34:51,862 ERROR [org.apache.struts.taglib.tiles.InsertTag] ServletException in '/wealth/portfolio.jsp': java.util.LinkedHashMap$Entry
org.apache.jasper.JasperException: An exception occurred processing JSP page /wealth/portfolio.jsp at line 178

175:                                           <bean:message key="html.advantageportfolio.columnname.yield"/>
176:                                     </html:link></span></td>
177:                         </tr>
178:                         <logic:iterate id="taxLotSecurityClassMap" name="sessionModel" property="taxLotDetailsMap" type="java.util.LinkedHashMap">
179:                               <tr>
180:                                     <td nowrap align="left" colspan="15">
181:                                           <span class="labeldark">${taxLotSecurityClassMap.key}</span>


Stacktrace:
      at org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:518)
      at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:429)
      at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
      at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
      ..................................................................
      at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:580)
      at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
      at java.lang.Thread.run(Thread.java:595)
Caused by: java.lang.ClassCastException: java.util.LinkedHashMap$Entry
      at org.apache.jsp.wealth.portfolio_jsp._jspService(portfolio_jsp.java:648)
      at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
      at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:387)
      ... 59 more

Thanks,
Raja

<logic:iterate id="taxLotSecurityClassMap" name="sessionModel" property="taxLotDetailsMap">
	<tr>
		<td nowrap align="left" colspan="15">
			<span class="labeldark">${taxLotSecurityClassMap.key}</span>
		</td>
	</tr>
	<c:set var="taxLotSymbolMap" value="${taxLotSecurityClassMap.value}"/>
	<c:forEach var="taxLotListMap" items="${taxLotSymbolMap}">
	<pg:item>
		<c:set var="symbol" value="${taxLotListMap.key}"/>
		<c:set var="taxLotList" value="${taxLotListMap.value}"/>
		<c:choose>
			<c:when test="${fn:length(taxLotList) == 1}">
				<c:set var="taxLotLine" value="${taxLotList[0]}"/>
			</c:when>
			<c:otherwise>
				<c:set var="taxLotLine" value="${portfolioTotals[symbol]}"/>
			</c:otherwise>
		</c:choose>
		<tr>
			.....................
			.....................
		</tr>
		<c:if test="${fn:length(taxLotList) > 1}">
			<logic:iterate id="taxLotDetail" indexId="taxLotIndexNumber" collection="${taxLotList}" type="com.vantra.volts.core.session.aspirewealth.portfolio.WealthPortfolioLine">
				<bean:define id="taxLotLineNumber" value="<%= String.valueOf(taxLotIndexNumber.intValue() + 1) %>" type="java.lang.String"/>
				<tr style="display: none;" id="<%=taxLotDetail.getSymbol() + taxLotLineNumber%>">
					....................
					....................
					....................
				</tr>
			</logic:iterate>
		</c:if>
	</pg:item>	
	</c:forEach>
 
	<c:if test="${fn:length(taxLotSecurityClassMap) > 1}">
		<tr>
			<td align="left" colspan="5" nowrap>&nbsp;</td>
			<td align="right" nowrap><hr></td>
			<td>&nbsp;</td>
			<td align="right" nowrap><hr></td>
			<td align="right" nowrap><hr></td>
			<td align="right" nowrap><hr></td>
			<td align="right" nowrap><hr></td>
			<td>&nbsp;</td>
			<td>&nbsp;</td>
			<td align="right" nowrap><hr></td>
			<td align="right" nowrap><hr></td>
		</tr>
 
	</c:if>	
 
</logic:iterate>
ASKER CERTIFIED SOLUTION
dolphin_g

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 3 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros