on 10-26-2007 7:53 PM
Hi Everybody,
I have a problem when I try to read data from a hashmap in a session.
This is how I fill my list:
IPortalComponentSession session = ((IPortalComponentRequest)this.getRequest()).getComponentSession();
Map lijst = new HashMap();
try
{
// get all ids from database
int[] ids = sd.getAllSkillIDs();
for(int i=0; i<6; i++)
{
// get a skill from the database and insert the id and the name
SkillBean s = sd.getSkillByID(ids<i>);
lijst.put(String.valueOf(s.getId()), s.getName().toString());
}
session.putValue("lijst",lijst);
}
This gives a runtime error when trying to read it in the jsp.
I'm sure that the object that is returned is filled and correct.
And when I create my list like this:
lijst.put("1","Value1");
lijst.put("2","Value2");
Then all the values contained in the list are printed.
This is my jsp
<%@taglib uri="tagLib" prefix="hbj"%>
<hbj:content id="myContext" >
<hbj:page title="PageTitle">
Skills in Database:<br/><br/>
<% Map lijst = (java.util.HashMap)componentRequest.getComponentSession().getValue("lijst");
for(int i=1; i<=lijst.size();i++)
{
%>
<%=String.valueOf(i)%> -=> <%=lijst.get(String.valueOf(i)).toString() %>
<br/>
<%
}
%>
</hbj:page>
</hbj:content>
Help me please
Thank you
Wouter
Hi Wouter Delellio ,
Your way of retriving value from the hashMap is not the right process as
At the time of putting values to the hashMap you are assigning key as follows :
// get all ids from database
int[] ids = sd.getAllSkillIDs();
for(int i=0; i<6; i++)
{
// get a skill from the database and insert the id and the name
SkillBean s = sd.getSkillByID(ids<i>);
lijst.put(String.valueOf(s.getId()), s.getName().toString());
}
But in the jsp when you are retriving the data fom the hashMap the key that you have taken is as follows :
for(int i=1; i<=lijst.size();i++)
{
%>
<%=String.valueOf(i)%> -=> <%=lijst.get(String.valueOf(i)).toString() %>
<br/>
<%
}
where the key you habe used is the i whose value in the followinf code is 1,2......n
You need to wrte the code in the following manner where first you get the <b>Key</b> and then you get the value for that key . The code goes like this :
<%@taglib uri="tagLib" prefix="hbj"%>
<hbj:content id="myContext" >
<hbj:page title="PageTitle">
Skills in Database:<br/><br/>
<% Map lijst = (java.util.HashMap)componentRequest.getComponentSession().getValue("lijst");
Set set = lijst.keySet();
Iterator it = set.iterator();
while (it.hasNext()) {
object obj = it.next();
String key = (String) obj.toString();
%>
<%=String.valueOf(key)%> -=> <%=lijst.get(key)%>
<br/>
<%
}
%>
</hbj:page>
</hbj:content>
Now i think you will be able to get the value from the hash map.
If it works please do reqard with points.
Thanks
Ritushree Saha
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi
When you are retrieving the values from the hashmap in your JSP you are using the loop for(int i=1; i<=lijst.size();i++) and in the loop you are trying to get the values by lijst.get(String.valueOf(i)). Here 'i' is the counter of your loop. So 'i' can take the values 1,2,3 .... etc. Now , when you pass 'i' as parameter to lijst.get(<key>) key has to be 1,2,3 ...etc according to this logic to fetch the values associated with them.
Now when you put the hardcoded values for the 'Key' and 'Value' in your hashmap by
lijst.put("1","Value1");
lijst.put("2","Value2");
then the retrieval of the values in JSP works fine because the logic in the above loop in your JSP gets the keys as 1 , 2 etc. and so the values associated with these keys are retrieved properly.
When you are putting the values of the skillsets in the hashmap you are using
lijst.put(String.valueOf(s.getId()), s.getName().toString())
Here are you sure that the Id has the values as 1,2,3...etc since your retrieval loop in JSP always passes the Key as 1,2,3... to lijst.get() method.
I feel that Id field of your skillset object takes some other values (which are not 1,2,3..) and hence you get runtime error.
I hope this helps you.
Regards,
Ajay
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
79 | |
9 | |
9 | |
7 | |
7 | |
6 | |
6 | |
5 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.