-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Cast selectItem to Entity
PostPosted: Wed Jul 02, 2008 4:23 am 
Regular
Regular

Joined: Tue Jun 03, 2008 1:12 pm
Posts: 84
Location: germany
I have this in my Entity:

private Set<Role> roles = new HashSet<Role>();

and this in my sessionBean:

private List<SelectItem> roles;

I retrieved values for Roles from a selectManyCheckbox:

Code:
20:23:36,484 INFO  [STDOUT] Element in set: entity.Role@f86dd86c: java.lang.String
20:23:36,484 INFO  [STDOUT] Element in set: entity.Role@a57fe861: java.lang.String


How can I cast from SelectItems to Role-Items or how can I retrieve the values from selectItems to the Set Roles in an elegantly way ???


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 03, 2008 2:18 am 
Regular
Regular

Joined: Tue Jun 03, 2008 1:12 pm
Posts: 84
Location: germany
I have tried it with that..

Code:
// go through the roles List which consists of java.lang.STRING-objects
for (Iterator i = roles.iterator(); i.hasNext();) {


Object m = (Object) i.next();

// and cast the next STRING-Objects to my ROLE-Objects
Role o = (Role) m.getClass().cast(role);

}



But the cast does NOT work. what is wrong?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 03, 2008 2:27 am 
Newbie

Joined: Thu Jun 26, 2008 12:50 am
Posts: 9
I'm not sure if it's what you're trying to achieve, but you need to set the role as the "value" on the SelectItem using SelectItem.setValue().

When JSF informs you when SelectItem was selected, you need to grab the "value" back using SelectItem.getValue() - and that Object should cast back to your Role class OK.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 03, 2008 3:37 am 
Regular
Regular

Joined: Tue Jun 03, 2008 1:12 pm
Posts: 84
Location: germany
I have done that before, but this does not work for me. I got an String-Object and cannot cast it.

I tried it with this:


// rolesGUI is the selected list from the the selectManyCheckbox
for (int i = 0; i < this.rolesGUI.size(); ++i){

// roles is my Role-Object-List
roles.add((Role) (rolesGUI.get(i)).getValue()) ;

}

I got an exception:

Code:
Caused by: java.lang.ClassCastException: java.lang.String
...


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 03, 2008 5:46 pm 
Newbie

Joined: Thu Jun 26, 2008 12:50 am
Posts: 9
Can you show the code where you're binging the roles to the RolsGUI also?

Have you looked at the content of the strings you're getting back?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.