*
The moose likes Struts and the fly likes [Struts] bean:define... - complex objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "[Struts] bean:define... - complex objects" Watch "[Struts] bean:define... - complex objects" New topic
Author

[Struts] bean:define... - complex objects

J.H.B. Oosterlaar
Ranch Hand

Joined: Sep 12, 2002
Posts: 41
Hi,
Another question from me, the Struts-guru-wanna-be :
I have stored a Vector of Topic objects in the request scope. A Topic has a Vector of Message objects. But for the topic displaypage, only the first Message counts. Now, of course I have these two methods in my Topic bean:


For the first Message retrieval I have written the following method:

When printing to standard output, everthing goes as expected, but no result comes up within the page. The localhost_log file gives notice of a ServletException, that is thrown by the getFirstMessage() method.
I define the bean from the Collection of Topic objects within the JSP page like this:

Now, I read that the define-tag can only define String objects, or indexed/listed Collections, so I tried to use the type="_._.Message" attribute. That didn't help either.
Do you know what I need to do? I could return a Collection with only one (the first one) Message object, but that would be quite lame.
Thanks and cheers,
Jeroen
Jason Menard
Sheriff

Joined: Nov 09, 2000
Posts: 6450
Are you simply trying to write out some property of the first message? If so, let's say the property of "firstMessage" that you want to write out is "title", in which case you can do something like this:
<bean:write name="topic" property="firstMessage.title"/>
Depending on how complex your scenario is, you might find nested tags the way to go, in which case you can try something like this:

HTH
[ January 14, 2003: Message edited by: Jason Menard ]
J.H.B. Oosterlaar
Ranch Hand

Joined: Sep 12, 2002
Posts: 41
Same problem. I get the following output of the exception:
javax.servlet.ServletException: Exception thrown by getter for property firstMessage of bean topic
at org.apache.runtime.PageContextmpl.handlePageException....
rootcause:
javax.servlet.jsp.JspException: Exception thrown by getter for property firstMessage of bean topic
at org.apache.struts.taglib.template.InsertTag.doEntTag(InsertTag.java:149).....
Jeroen
Jason Menard
Sheriff

Joined: Nov 09, 2000
Posts: 6450
Originally posted by J.H.B. Oosterlaar:
rootcause:
javax.servlet.jsp.JspException: Exception thrown by getter for property firstMessage of bean topic
at org.apache.struts.taglib.template.InsertTag.doEntTag(InsertTag.java:149).....

It looks like you are using the template tag library. I have never used that taglib so I can't really go into any particulars about it, however the template taglib has been deprecated in Struts 1.1 in favor of tiles.
The first thing I would do is make one composite jsp (no templates) and see whether or not you get the same error. At least that will narrow it down a bit.
The other thing I can suggest is to show us the code for your beans and your jsp and we might be able to spot something.
J.H.B. Oosterlaar
Ranch Hand

Joined: Sep 12, 2002
Posts: 41
Still the same, I'll give some listing:
Exception rootcause (when no template is used):

The JSP file:

Piece of code from Topic:

Hope this helps...
[ January 14, 2003: Message edited by: J.H.B. Oosterlaar ]
Jason Menard
Sheriff

Joined: Nov 09, 2000
Posts: 6450
In your JSP code, try this:

Change the <bean:write> and you can get rid of <bean:define>. Not sure if it will make a difference, but you could add the attribute type="mypkg.Topic" to your logic:iterate.
Something else that might possibly cause this problem is mentioned in the API docs:
If the collection you are iterating over can contain null values, the loop will still be performed but no page scope attribute (named by the id attribute) will be created for that loop iteration. You can use the <logic:present> and <logic:notPresent> tags to test for this case.

Let me know if any of that works.
Jason Menard
Sheriff

Joined: Nov 09, 2000
Posts: 6450
One more thing... make sure you don't have any other beans named "topic" anywhere on your page.
J.H.B. Oosterlaar
Ranch Hand

Joined: Sep 12, 2002
Posts: 41
Jason,
Still the same problem:
*) Using the bean:write with firstMessage.title
*) Setting the type of the iterator
The null-issue is not the problem. There isn't a null value in the Collection ever.
I find this very strange. It is just a simple bean that is returned. Nothing more.
Jeroen
Jason Menard
Sheriff

Joined: Nov 09, 2000
Posts: 6450
Just to see what happens, go ahead and try changing the id "topic" to something different, like "myTopic" maybe.
Jason Menard
Sheriff

Joined: Nov 09, 2000
Posts: 6450
Jeroen,
I through together some test code of my own to see if I could duplicate your error. Here is the exact code I used in all the gory details:
index.jsp (start here)

struts-config.xml (excerpt)

Topic.java

Message.java

TopicAction.java

topic.jsp

(I only put in those "/* */" empty comments to keep some semblence of formatting because the "CODE" tag in UBB collapses blank lines)
Anyway, upon running index.jsp and clicking the link, everything worked fine and I received what you might expect:

So my suggestion now is to look very closely at all your code and see if maybe something was misspelled somewhere, or if there are any obvious differences between what I am doing and what you are doing.
Let me know how it works out for you.
J.H.B. Oosterlaar
Ranch Hand

Joined: Sep 12, 2002
Posts: 41
Hi Jason,
I have it! Because of a database designflaw, I was forced to alter the Topic bean, so I didn't need to use the Message bean anymore. That had something to do with the first message poster being the topic starter, which of course is nog a good setup, since message can be deleted. When the first message was to be deleted, then the author of the second message, would be the topic starter.
Anyway, I now have added a beanproperty user of the User type (which is the bean that holds the User information). I used the exact same construction (<bean efine id="user" name="topic" property="user"/> instead of the firstMessage) and I worked! I guess the Message is not allowed for some reason (probably a Struts object).
Thanks for you help!
Jeroen
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: [Struts] bean:define... - complex objects