wood burning stoves 2.0*
The moose likes Struts and the fly likes Passing objects in struts2 <s:select Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Passing objects in struts2 <s:select" Watch "Passing objects in struts2 <s:select" New topic
Author

Passing objects in struts2 <s:select

eddy johns
Ranch Hand

Joined: Feb 16, 2010
Posts: 67
Hi all,

A newbie question. I have an <s:select set like this:

It renders fine on the page. Now I want to retrieve the selected doctor on my next page. But all I'm able to get is a String, which comes from the listKey. On my nextPage.jsp I have this:


If I change the type from String to Person, like this:

I get a

caused by


I could attach the whole stacktrace but it seems like my question is quite basic and I don't want to spam the post with it. So what's going on? How can I pass the selected Person object from one page to another? Right now I'm using the personID to get the object from the database but it's a bit ridiculous.


Thanks,
Eddy
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9305
    
  17

Well using the id is generally the way to do it. You can create a custom type converter which will take the id and retrieve the related Person class object from the database (or wherever all Person objects are stored)...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
eddy johns
Ranch Hand

Joined: Feb 16, 2010
Posts: 67
Hi Ankit, thanks for writing back. So there's absolutely no way to pass objects? Not even on the request or the session?

Thanks,
Eddy
Jaimesh M Ponkia
Greenhorn

Joined: Nov 24, 2010
Posts: 21
eddy johns wrote:Hi Ankit, thanks for writing back. So there's absolutely no way to pass objects? Not even on the request or the session?

Thanks,
Eddy


If you are looking to avoid database call then there are few ways to do so:
1. Implement caching mechanism in your application, where in during the first database call you can store the result in a map and next time using the key you can fetch the object back from the map.
2. If you are fine using the session object, then you can put the result from the first database call into session and later during the second submit, use the key to fetch the value back from the session object.
eddy johns
Ranch Hand

Joined: Feb 16, 2010
Posts: 67
Hi Jaimesh, thanks for writing. Yes, I can see that these options would work. But still, disappointed I can't simply retrieve the object through the framework. It should be possible in our day and age..
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9305
    
  17

What way do you expect to retrieve the Person object?? As Jaimesh said, you can store the Person list in a session. You can't know in advance which person the user will select so you'll have to store the whole list in the session. The other way is to retrieve the Person object through its ID and I told you how to do that in a modular way so that you can reuse the same code...
eddy johns
Ranch Hand

Joined: Feb 16, 2010
Posts: 67
Just like the framework (struts) is passing the id, it can pass the object. So for example itemValue (or any other name) can carry the Person object. Other frameworks do that. For example Apple's WebObjects did just that over 10 years ago.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Passing objects in struts2 <s:select