• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

In JSP : Retrieve data from a hashmap

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I have a bean in my jsp and one of it's properties is a hashmap of objects. I'd like to go over the hashmap and check whether a particular key exists.
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").trim());
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 should be able to retrieve the objects with a
String literal. Ie. if the form name is "manager", get("manager") will return the corresponding FormRole object. I should be able to say containsKey("manager") and get "true". That is not what's happening:

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 resent 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 resent>
<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 resent scope="session" name="user">
<li><html:link forward="logoff">Sign out</html:link></li>
<br>
<br>
<bean efine 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 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 therwise>
NOT Found
</c therwise>
</c:choose>

</logic resent>
</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. Notice that fRoles.containsKey("manager") return false. Why???
Also notice "NOT Found" that comes from <c:when test= '<% fRoles.get("manager") != null %> '>.
Someone on google suggested that there are trailing white spaces. So made sure I had fRoles.setFormName(rs.getString("FormName").trim());
I also tried when test with "manager ".
Nothing works. I'd appreciate if you could help me.

Thanks in advance.

NK
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you check the key. It may be "manager "

As I see from your code you are doing trim while creating object
fRoles.setFormName(rs.getString("FormName").trim());


While putting object in the hashmap you are not doing trim.
Is it possible key used for get may be trimmed?

Thanks
 
Nadia Kunkov
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you!!!
That was it. I didn't have trim() when adding it to a hashmap.
You have saved me a lot of grieve.
Appreciate your help.
Thanks again.
NK
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic