File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Struts and the fly likes Accessing a beanList Returned from JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Accessing a beanList Returned from JSP" Watch "Accessing a beanList Returned from JSP" New topic
Author

Accessing a beanList Returned from JSP

C Trager
Greenhorn

Joined: Aug 04, 2010
Posts: 11
I'm new to Struts2 (worked on some Struts1 about 6 years ago) and I'm creating a new app from scratch. I'm running some stored procedures and building a beanList (of varying count) which I'm displaying through a JSP with each bean in the ArrayList displaying on the screen properly. However, when I click a submit button on the page I am wanting to get that beanList back and iterate through the beanList returned from the JSP (one of the bean properties is displayed in a textbox which the user can change the contents of) comparing with one I save in the session to see if there are any changes. Unfortunately the beanList in my action is null on returning from the JSP. If I remember correctly Struts1 handled passing that information back without me having to write code. Am I missing something simple here?

Here's the action (tried to make it more readable by just putting in comments for large chunks of code, the first line in the execute is what I'm trying to figure out).



Here's the JSP code. I removed some to make it easier to read. It does display the rows correctly, and I am able to edit each msgId text field for each row.



Sorry for the noob question, but I must be missing something very simple here as it seems I should be able to get an array of beans (or properties) back from the JSP. If not how and I supposed to get a list of items back from the JSP?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

I think you need to either (1) preface the input element name with the name of the list, or (2) same, but use indexed (array) notation to preserve order... as far as I know that still needs to be done manually (the old S1 indexed property tags added it for you). If you look at the rendered HTML you'll see why.
C Trager
Greenhorn

Joined: Aug 04, 2010
Posts: 11
David Newton wrote:(1) preface the input element name with the name of the list,

Are you refering to something like this? When I tried this it no longer recognized that property when loading the JSP.


David Newton wrote: (2) same, but use indexed (array) notation to preserve order... as far as I know that still needs to be done manually (the old S1 indexed property tags added it for you). If you look at the rendered HTML you'll see why.

I'm still trying to translate this into something that I can understand looking through the rest of the forums. To bad there aren't any examples that can easily be Googled. :(

David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

http://struts.apache.org/2.x/docs/type-conversion.html#TypeConversion-RelationshiptoParameterNames
Richard Golebiowski
Ranch Hand

Joined: May 05, 2010
Posts: 213

I think it should be:
C Trager
Greenhorn

Joined: Aug 04, 2010
Posts: 11
Richard Golebiowski wrote:I think it should be:


That brought me a step closer (though I used status.index as I was already using that to number each line.) So, each field is populating correctly on the JSP for each row. When I return the beanList is initialized to the number of rows on the screen so we know that's it's recognizing that there should be something there; unfortunately there's nothing in the beanList (i.e. I show an array of 36 null values.) Making a change in the msgId textbox doesn't do anything either.

@David - Thanks for the link. It looks like the code from Richard is the same as this item from that link:

"For lists and maps, use index notation, such as people[0].name or friends['patrick'].name. Often these HTML form elements are being rendered inside a loop. For JSP Tags, use the iterator tag's status attribute. For FreeMarker Tags, use the special property ${foo_index}[]. "
Richard Golebiowski
Ranch Hand

Joined: May 05, 2010
Posts: 213

Can you check that it's hitting the setter when you return.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

S2 has no idea what's contained in that list since it's not generified and I'm assuming you haven't created any type conversion configuration.
C Trager
Greenhorn

Joined: Aug 04, 2010
Posts: 11
Richard Golebiowski wrote:Can you check that it's hitting the setter when you return.


I toggled a breakpoint on the beanList getter/setter, then I went into the getter/setter for msgId in the bean itself and toggled a breakpoint. I don't see where it's hitting the setter for the msgId on the return. However, I'm also see this error for each bean in the list:

[8/4/10 14:31:49:648 CDT] 0000003d ParametersInt E com.opensymphony.xwork2.interceptor.ParametersInterceptor setParameters ParametersInterceptor - [setParameters]: Unexpected Exception caught setting 'beanList[10].msgId' on 'class foo: Error setting expression 'beanList[10].msgId' with value '[Ljava.lang.String;@54e054e0'

@David - No, I haven't created a type conversion (I'm researching that in my Struts2 In Action book right now.)
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Can't you just make it generic?
C Trager
Greenhorn

Joined: Aug 04, 2010
Posts: 11
David Newton wrote:Can't you just make it generic?


Make the bean generic?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

No, the list. Making the bean generic doesn't make any sense.
Richard Golebiowski
Ranch Hand

Joined: May 05, 2010
Posts: 213

Add the type parameter to your declaration.

C Trager
Greenhorn

Joined: Aug 04, 2010
Posts: 11


I do make the List generic, then I take my beans and add them to the list.
Richard Golebiowski
Ranch Hand

Joined: May 05, 2010
Posts: 213

see generics
C Trager
Greenhorn

Joined: Aug 04, 2010
Posts: 11
Richard Golebiowski wrote:see generics


It works! I'm now able to see the beanList array filled in with my new msgId immediately when I enter the action. I'm assuming when it is made generic then it keeps whatever struts does in the background from putting anything other than my specific bean type into that array. I'll have to dig into generics more as I'm not that familiar with them.

Thanks a ton for the help guys, now I can stop banging my head against my desk and move on to how to use a checkbox in my beanList.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Accessing a beanList Returned from JSP