Retrieving data from a hashmap from within a JSP

K

kbd

Hello:

I can not understand what I have done wrong. I need to test if a
key/value pair exists in a hashmap from within a JSP. I though this
would be simple. Struts is also involved here, but I do not believe
that Struts is causing any problems.

The key/values are placed in the hashmap as follows:
hMap = new HashMap(20);
while ( rs.next() ) {
fRoles = new FormRoles();
fRoles.setFormName(rs.getString("FormName"));
fRoles.setCreate( ( rs.getInt("C")==1)? true:false );
fRoles.setRead( ( rs.getInt("R")==1)? true:false );
fRoles.setUpdate( ( rs.getInt("U")==1)? true:false );
fRoles.setDelete( ( rs.getInt("D")==1)? true:false );
hMap.put(rs.getString("FormName"), fRoles);
} //while
user.setFormRoles( hMap);
In the above code I place the FormRoles objects into the hashmap with
a String as the key. I will be able to retrieve the objects with a
String. Ie. if the form name is "manager", get("manager") will return
the corresponding FormRole object.
Correct??

Here is my JSP:

<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="c_rt" uri="http://java.sun.com/jstl/core_rt" %>

<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>

<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>

<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>



<html>

<head>

<title>Welcome World!</title>

<html:base/>

</head>

<body bgcolor="white">
<logic:present scope="session" name="user">
<h3>Welcome <bean:write name="user" property="userId"/>!</h3>
<br>
<bean:write name="user" property="userFName"/>
<bean:write name="user" property="userLName"/>
</logic:present>
<logic:notPresent scope="session" name="user">
<h3>Welcome World!</h3>
</logic:notPresent>
<html:errors/>

<ul>

<li><html:link forward="logon">Sign in</html:link></li>
<logic:present scope="session" name="user">
<li><html:link forward="logoff">Sign out</html:link></li>
<br>
<br>
<bean:define id="fRoles" name="user" property="frmRoles"
type="java.util.HashMap" toScope="page"/>
<logic:iterate id="element" name="fRoles">
Key is <bean:write name="element" property="key"/> <br>
Value is <bean:write name="element" property="value"/> <br>
</logic:iterate>

<c:forEach var="element" items="${fRoles}">
<c:eek:ut value="Key=${element.key}, Value=${element.value}"/>
<br>
</c:forEach>

fRoles.size() returns <%= fRoles.size() %> <br>
fRoles.keySet() returns <%= fRoles.keySet() %> <br>
fRoles.containsKey("manager") returns <%=
fRoles.containsKey("manager") %>
<br>
<c:forEach items="${fRoles}" var="item">
<c:if test="${item.key == 'manager'}">
Found manager
</c:if>
</c:forEach>

<c:choose>
<c:when test= '<% fRoles.get("manager") != null %> '>
Found formRoles manager
</c:when>
<c:eek:therwise>
NOT Found
</c:eek:therwise>
</c:choose>

</logic:present>

</ul>





<img src='struts-power.gif' alt='Powered by Struts'>



</body>

</html>



Here is the resulting page:

Welcome BC!

B C

* Sign in
* Sign out


Key is country
Value is com.parkerglobal.mgrappBusiness.FormRoles@a2e19e
Key is manager
Value is com.parkerglobal.mgrappBusiness.FormRoles@18a62f6
Key is region
Value is com.parkerglobal.mgrappBusiness.FormRoles@6742d0
Key is currency
Value is com.parkerglobal.mgrappBusiness.FormRoles@1539d49
Key is classificationType
Value is com.parkerglobal.mgrappBusiness.FormRoles@7b5cb8
Key is managerFundClass
Value is com.parkerglobal.mgrappBusiness.FormRoles@1046270
Key is pgsPortfolio
Value is com.parkerglobal.mgrappBusiness.FormRoles@dad4b8
Key is pgsMgrAllocations
Value is com.parkerglobal.mgrappBusiness.FormRoles@c39410
Key is pgsPortfolioShareClass
Value is com.parkerglobal.mgrappBusiness.FormRoles@2db73e
Key is managerFund
Value is com.parkerglobal.mgrappBusiness.FormRoles@1594ba3
Key is classification
Value is com.parkerglobal.mgrappBusiness.FormRoles@174f478
Key=country , Value=com.parkerglobal.mgrappBusiness.FormRoles@a2e19e
Key=manager , Value=com.parkerglobal.mgrappBusiness.FormRoles@18a62f6
Key=region , Value=com.parkerglobal.mgrappBusiness.FormRoles@6742d0
Key=currency ,
Value=com.parkerglobal.mgrappBusiness.FormRoles@1539d49
Key=classificationType ,
Value=com.parkerglobal.mgrappBusiness.FormRoles@7b5cb8
Key=managerFundClass ,
Value=com.parkerglobal.mgrappBusiness.FormRoles@1046270
Key=pgsPortfolio ,
Value=com.parkerglobal.mgrappBusiness.FormRoles@dad4b8
Key=pgsMgrAllocations ,
Value=com.parkerglobal.mgrappBusiness.FormRoles@c39410
Key=pgsPortfolioShareClass ,
Value=com.parkerglobal.mgrappBusiness.FormRoles@2db73e
Key=managerFund ,
Value=com.parkerglobal.mgrappBusiness.FormRoles@1594ba3
Key=classification ,
Value=com.parkerglobal.mgrappBusiness.FormRoles@174f478
fRoles.size() returns 11
fRoles.keySet() returns [country , manager , region , currency ,
classificationType , managerFundClass , pgsPortfolio ,
pgsMgrAllocations , pgsPortfolioShareClass , managerFund ,
classification ]
fRoles.containsKey("manager") returns false
NOT Found


This tells me that I have a valid hashmap object. But why, oh why
does fRoles.containsKey("manager") return false. I am stumped.

Thanks much for looking at this issue.

best regard.

kd
 
K

Kieron Briggs

kbd said:
[... snip ...]
I will be able to retrieve the objects with a
String. Ie. if the form name is "manager", get("manager") will return
the corresponding FormRole object.
Correct??

So far so good...
[... snip ...]
fRoles.keySet() returns [country , manager , region , currency ,
classificationType , managerFundClass , pgsPortfolio ,
pgsMgrAllocations , pgsPortfolioShareClass , managerFund ,
classification ]
[... snip ...]

It looks to me like you might have some trailing whitespace on the
strings used as the key of the hashmap. And, since "manager " !=
"manager", your call to containsKey returns false.

Try adding the elements to the hashmap with
hMap.put(rs.getString("FormName").trim(), fRoles);
^^^^^^^
and see if that helps.


Home this helps,

Kieron
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top