Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

form values lost in request scope

 
sai kinnera
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am using iterate tag to display my collection as:

<logic:iterate id="books" name="xxForm" property="books" type="model.BookDTO" indexId="index" >
<html:checkbox name="books" property="include" indexed="true" styleClass="field_name" value="true" />
<bean:write name="books" property="bookName" />

The collection is loaded into the form when I first get the page and works fine. Then if I select any of the books, the action is to be repeated and certain particulars of the book are to be populated on the same page. The problem is: if I keep the scope as session everything is fine, but if the scope is request, the form data is lost even before the reset is called and the control is not going into the action handler. It's giving ArrayIndexOutofBoundsException:0 index 0. I think it's because books field(collection) in the form has 0 size as if the form itself is being initiated with 'new'. All other values in the form are also defaults.

Can anybody please advise how could this happen? using struts1.1.

Thanks in advance,
sai Kinnera
 
Jason Berk
Ranch Hand
Posts: 41
 
Brent Sterling
Ranch Hand
Posts: 948
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I read over the link. I would agree that the preferred method (Manually call validate and use Request Scope) is pretty good and maybe a reason for me to switch over to DispatchAction or at least update my code so that the "display" and "save" actions are implement in a single java file.

I use a different technique. I drive different functionality of my "display" action by configuring multiple action mappings that use the same action class but pass in different parameter values. The parameter is "Refresh" then the action know that it just needs to retrieve the dynamic data for the page (such as lists). My save action would be configured to use the "refresh" action for the input parameter.

- Brent
 
Soumya Saha
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sai,

Can you access the Logic for ur Collection Population every time
you submit the form?

If so u can just take out the user selection and get the corresponding
details from ur collection and display it.

Thanks
 
sai kinnera
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the replies. Jason & Brent I have gone throught the content in the link, but my problem is not a validaion failure. Even before calling validate(), reset() gets called and by this time itself the form is losing all the data.
Hope you unserstood Soumya, when the form is losing data, how can we access
the collection. It's case of the form being in session and everything works fine. Except that the servers are loaded with huge session data and so I am trying to replace with request scope.

Thanks,
Sai
 
Soumya Saha
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sai,

What I meant was 'Logic For Collection Population' :

..That is the Step where u might be doing some
Database Query to generate ur Books Collection or used some method,

Not the Collection List u have displayed on ur Page..Of Course u don't have it on submission..

Thanks
 
Brent Sterling
Ranch Hand
Posts: 948
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe it would help if you described the mechanism that you have in place in more detail. It sounds like your page has a list of books, and when the user selects one of the books then properties of the book (maybe cost, author and publisher) are shown on the page. When a book is selected, do you submit the page? If so, how do you detect that the user selected a book instead of clicking the "save" button?

Here is what I do for this...When a book is selected a script sets the value of the "refreshControl" property. My base action class for handling saves detects that this value is set and then forwards to the input property instead of saving the record. The input property is defined to call the "refresh" action. The refresh action takes care of loading dynamic values and in this case it might query the database to get the properties associated with the selected book. I am not sure if this makes any sense.

Since the submission and display are all on the same request, all the values that the user has entered into the form are retained along with any hidden fields. You just need to make sure that any values that are not submitted with the form are populated during the refresh processing.

- Brent
 
sai kinnera
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Brent,
I was temporarily moved to a different problem. Coming back, I get the page with a list of books(each book is a BookDTO with all particulars). The list is shown on the page with a check box and book name. When the user selects a paricular book and submits this request struts is giving a problem of ArrayIndexOutOfBoundsException. The selection is maintained in the checkbox.
The form is losing the list of books(ArrayList) and so we are getting that exception. The other string parameters in the form are in tact.
Thanks
Sai
 
Soumya Saha
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sai,
If the Form has to be kept in the request scope, then to maintain the arrayList u have to call the method from which u are generating the arrayList everytime.

Else u can try it out with multibox.

U have to declare a String Array or int Array in ur Form.

Action Form:

private String[] selectedItems;

public String[] getSelectedItems() { return selectedItems; }

public void setSelectedItems(String[] selectedItems) { this.selectedItems = selectedItems; }


In JSP:
Where u are declaring a checkbox: declare the multibox

<logic:iterate id="books" name="xxForm" property="books" type="model.BookDTO" indexId="index" >
<html:multibox property="selectedItems">
<bean:write name="books" property="bookName" />
</html:multibox>

When u submit the Form u will get the user selection values from the multibox property.

Thanks
 
Brent Sterling
Ranch Hand
Posts: 948
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the little that I can tell about your page, I would agree with the solution posted by Soumya. If you are not editing the properties of a book in place then I don't see a need to used indexed properties. Index properties would be used if you had a list of books on your page and the end user could edit the price of each book and then save all the updated prices at once.

- Brent
 
sai kinnera
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Soumya/Brent,
I too think this might work. But I have to see how I can maintain the association between the selected ones and the corresponding BookDTOs. I have to go with checkboxes as per our screens. Thanks for the lead.

Sai
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic