aspose file tools *
The moose likes Struts and the fly likes Accessing an object within a Set in a bean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Accessing an object within a Set in a bean" Watch "Accessing an object within a Set in a bean" New topic
Author

Accessing an object within a Set in a bean

Kimbo Inatl
Greenhorn

Joined: Jun 30, 2010
Posts: 11
I have a bean called FacilityDTO that contains a set of ContactDTO objects. I want to do something similar to this:



using the "." notation (e.g. medicalOpportunityDTO.name), but I want to access the first object in the Set of ContactDTOs... I imagine it would be something like:



I know I could just create a new bean from the Set, but for what I'm doing I can't do it that way because it won't work. I need to specifically reference it by facilityDTO as the name and give it a property that will point to the first object in the Set. Anyone have any ideas?
Richard Golebiowski
Ranch Hand

Joined: May 05, 2010
Posts: 213

You should be able to access this like you would any set in Java.

I haven't done this in Struts 1, but in Struts 2 I can do the following:



and the class:



then in the JSP page:



Kimbo Inatl
Greenhorn

Joined: Jun 30, 2010
Posts: 11
I ended up figuring it out eventually, but it's good to see that I did it the right way Thanks for the reply.

One problem I still seem to be having, though, is that I'm extending ValidatorActionForm with my FacilityDTO class (so that I can use it to get data from the form), and when I do this:



it works, and it sets values in it from the form (even from objects contained in the FacilityDTO object), but it doesn't seem to be setting values in my set. Here are the methods I added to my FacilityDTO class to set/get values from the set:



contacts is the Set in FacilityDTO. The getContactIndexed method works just fine, but the setContactIndexed doesn't seem to be working.
Richard Golebiowski
Ranch Hand

Joined: May 05, 2010
Posts: 213

Struts needs to be able to have a direct path for the getters and setters in your classes.
See this for an explanation.
You cannot return and set a complete object from the JSP page. Struts needs to be able to get the complete object and then get or set the object fields.
Kimbo Inatl
Greenhorn

Joined: Jun 30, 2010
Posts: 11
Richard Golebiowski wrote:Struts needs to be able to have a direct path for the getters and setters in your classes.
See this for an explanation.
You cannot return and set a complete object from the JSP page. Struts needs to be able to get the complete object and then get or set the object fields.


OK... To clarify, this works (medicalOpportunity is an object inside of facilityDTO):



But this does not (contacts is a Set within facilityDTO, and I am attempting to access the first object in the Set and set the contact's first name via the setter/getter methods I posted above to access the Set via an index):



So I'm not trying to set an entire object, I'm using the getter/setter methods. Actually, it does work for filling in the data in the form, but it doesn't work for retrieving the data from the form, thus I am guessing that there is something wrong with the setter method I wrote.

EDIT: Oh, wait, I see what you're saying. It is essentially doing this:



So the setter method I wrote isn't actually being called. Still not 100% sure why this isn't working though.



Kimbo Inatl
Greenhorn

Joined: Jun 30, 2010
Posts: 11
Changed my getter method to this, it works now.



Thanks Richard!
 
Don't get me started about those stupid light bulbs.
 
subject: Accessing an object within a Set in a bean