This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Struts and the fly likes How to retain automatically generated fields on pageload in struts2 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "How to retain automatically generated fields on pageload in struts2" Watch "How to retain automatically generated fields on pageload in struts2" New topic
Author

How to retain automatically generated fields on pageload in struts2

Vighnesh Mu
Greenhorn

Joined: Jan 06, 2011
Posts: 29

Hi,

I am designing a webpage using "Struts 2" as given in the "screenshot.jpg" so when I click on the "Add Row" a new row will be added and when I select a particular check box in a row and click on "Delete Row" it will delete that particular row as in "screenshot2.jpg". But I am facing a problem here that when I make entry to all fields as in "screenshot3.jpg" and click on "Add details",the page reloads and every data will come in a single row instead of multiple rows as I entered earlier. So If I do validation and display errors to correct the value it's very difficult to do the same as all data will be in a single row as shown in "screenshot4.jpg". I have attached the "addComponent.jsp" also for reference, in POJO i am using string array for all fields to retrieve value from jsp. Please help me to resolve this issue.



Thank you.


[Thumbnail for screenshots_file.jpg]

Shashank Ag
Ranch Hand

Joined: Dec 22, 2009
Posts: 88

Well yes, this was a tricky one. Its probably some bug which I noticed in struts 2.0.6 (2 years back), I am surprised to know its still carried.
Solution is simple, instead of array in your action class use List<String>.

Also, there is a mistake on your side, that inside the form tag there sould be an iterator to generate the correct number of rows.
Hope it helps.


SCJP 91%, SCWCD 97%
Vighnesh Mu
Greenhorn

Joined: Jan 06, 2011
Posts: 29

Thanks for the reply.

Solution is simple, instead of array in your action class use List<String>.

As you suggested I replaced all string array for each input field with List<String>, but the problem persist. I mean after form submit every value will come in a single row. Can you please tell me do I need to change anything other than this?

Also, there is a mistake on your side, that inside the form tag there sould be an iterator to generate the correct number of rows.

I don't want restrict the number of rows user can add, so if I use Iterator I should fix number of rows a user can enter rite? So I am doing through javascript. If I use iterator a fixed number of rows will be generated at the page load and user can't add extra row by clicking on "Add row" button right?

Please help me on this.
Shashank Ag
Ranch Hand

Joined: Dec 22, 2009
Posts: 88

I don't want restrict the number of rows user can add, so if I use Iterator I should fix number of rows a user can enter rite? So I am doing through javascript. If I use iterator a fixed number of rows will be generated at the page load and user can't add extra row by clicking on "Add row" button right?

No, not at all.
You should run iterator uptil the size of list and it will work fine.
Vighnesh Mu
Greenhorn

Joined: Jan 06, 2011
Posts: 29

Shashank Ag wrote:
I don't want restrict the number of rows user can add, so if I use Iterator I should fix number of rows a user can enter rite? So I am doing through javascript. If I use iterator a fixed number of rows will be generated at the page load and user can't add extra row by clicking on "Add row" button right?

No, not at all.
You should run iterator uptil the size of list and it will work fine.


But how will I know the size of list to create iterator? And initially the list will be null during first page load ,when user clicks on "Add row" a row will be added and so on it will continue. So when I click on "Add details" page will be reloaded and at that time I can have iterator as I will be having a list for input values from user but how to accomplish this at first start when list is null and it should be growing when user adds each row?

Thanks
Vighnesh Mu
Greenhorn

Joined: Jan 06, 2011
Posts: 29

Can somebody help me on accomplishing this task as I am stuck in the middle and unable to find help in web also.

Thanks in advance.
Shashank Ag
Ranch Hand

Joined: Dec 22, 2009
Posts: 88

You can simply write a getter method in your action class and put the logic of is list==null?0:list.size().
Call this getter mentod in in iterator tag's intialization.
Vighnesh Mu
Greenhorn

Joined: Jan 06, 2011
Posts: 29

Shashank Ag wrote:You can simply write a getter method in your action class and put the logic of is list==null?0:list.size().
Call this getter mentod in in iterator tag's intialization.


Hi Shashank as you suggested I replaced all string array with list for individual fields and got the same result as earlier. I am not getting how to use Iterator for this functionality, everywhere in web they showed how to use iterator to populate list of values from action but not the other way as in my case. Can you please show me code snippet to accomplish this task as I am struggling with this or any site for reference?.

Thanks,
Vighnesh
Vighnesh Mu
Greenhorn

Joined: Jan 06, 2011
Posts: 29

If anybody knows how to resolve this issue please help me.

Thanks
Tom Rispoli
Ranch Hand

Joined: Aug 29, 2008
Posts: 349
Seeing your POJO might help.
Vighnesh Mu
Greenhorn

Joined: Jan 06, 2011
Posts: 29

Tom Rispoli wrote:Seeing your POJO might help.


Hi Tom,

Here is my POJO and action class -

POJO -
Action class -
I also tried String array instead of list in POJO but got same result. I hope you can help me to resolve this issue .

Thanks.
Tom Rispoli
Ranch Hand

Joined: Aug 29, 2008
Posts: 349
Normally when I do something like this in my POJO I would make a class that holds 4 string variables (partNumber, componentDescription,quantity, unitPrice,totalPrice) and then I would make a single list out of objects of that class. I think the jsp code below should work for the way you have things set up here, as long as all of the lists are of the same lenght.



When I write addrow javascript like what you have, I put an index in the name of the input fields so each one can be uniquely identified. I'm not sure what will happen if the input fields created by the JSP have indexes in the names and are mixed with the ones created by the javascript. When the form is submitted, I don't know how struts will handle populating your POJO in that situation.
Vighnesh Mu
Greenhorn

Joined: Jan 06, 2011
Posts: 29

Hi Tom,

Thanks for the quick reply.
Normally when I do something like this in my POJO I would make a class that holds 4 string variables (partNumber, componentDescription,quantity, unitPrice,totalPrice) and then I would make a single list out of objects of that class.
I didn't get this part.
I already tried whatever you have given, when I replaced the old jsp code for "datatable" with the new one provided by you nothing appeared on the UI except 3 buttons when the page loads.So tried keeping both the codes and this is what happened -
1)When the page loads for the first time a single row will come as earlier and when I click "Add row"/"Delete row" button rows will be added/deleted as normal. But when I enter data into the fields and click on "Add details" validation will happen and all rows will also come along with an extra row at the top with all values.
2) Now if I add an extra row and click on submit it won't take the values in that extra row and when the page reloads that newly added row will not be available. In the same way I can't delete any row which was added earlier. Please refer the screen shot file for more details.

Please help me to resolve this.



[Thumbnail for Untitled.jpg]

Tom Rispoli
Ranch Hand

Joined: Aug 29, 2008
Posts: 349
The code for the data ojects would looks something like this:



Then in addComponentsVO you can create just one list like this:

private List<MyDataObject> myDataList;

With the appropriate getters and setters.

In your jsp the tex fields should be given names like

addComponent[%{#listStatus.index}].partNumber

The reason that you are seeing all of your values on that first line is the name attribute you are giving for the text field is the name of the entire array, not an element in it. If you want a blank row to show up initialy then you either need to poplulate the first element in the array (or in your case, each of your arrays) in your action or, write some javascript to add a blank row when the page is opened, but the row will need to have an index in the name.

I think having un-indexed field names is the root of your problem here, I think you should set them up to have indexes in them. Thats how I do it, unless someone else has a different approach to recomend that doesn't involve indexing.
Vighnesh Mu
Greenhorn

Joined: Jan 06, 2011
Posts: 29

Hi Tom,

About new solution -
As you told I replaced all List in addComponentsVO with just String variables and created an List obeject in action class like "List<addComponentsVO> addComponent ;" along with getters and setters and maintained the same jsp code of 2 datatables(one normal and one with iterator). But I am unable to receive the values eneterd by user in action class and after page reloads only one row(default row created by normal datatable) will come without any values.

About previous solution -
In previous post I said like I can't delete/add a row once user submit the form for first time,can you tell me how to resolve this? And moreover if the page reloads after validation only the user entered number of rows should come on UI but here I am getting an extra row along with number of rows enterd by users and I can't delete any row in that and if I add extra row also I can't get the value. I think the iterator is usable only if I want to generate a page using an already existing collection object, but in my case I won't be having any collection initially when the page loads first time, so how iterator part will work that's why I had normal "datatable" code also(but it's the main reason for that new row when page reloads). So please tell me do I need to have both "datatable" jsp code or only iterator part is enough? If only one part is enough how to achieve my functionality?

I am really frustrated by this,as I have seen in many websites this functionality and wondering how they implemented this?

I hope can resolve this soon. Any help is appreciated. Thanks in advance.
Tom Rispoli
Ranch Hand

Joined: Aug 29, 2008
Posts: 349
I'm pretty sure your problem is that default row, I'm assuming its unindexed. If thats the case I think the value from it will overwrite the entire array and you will then lose all of the other values. Like I said, if you want a blank row to appear, you either need to put in an extra element in you list in the action class or you need to have your javascript add it when the page loads.

I'm not sure why your add and delete don't work, but thier a good chance that your javascript will need to change to deal with the JSP changes. When it creates new rows it needs to put an index in the name. Try to track down where the js is going wrong, if you need help correcting it, post the js again with details on where its going wrong.

I do this kind of stuff all the time (move data from a list to an html page and back from the page to the list), the biggest difference between the way I do it and the way you are trying to do it is in my JSP I always reference individual elements in my list (using indexes) ratherer than referencing the full array.

Tom Rispoli
Ranch Hand

Joined: Aug 29, 2008
Posts: 349
I'm pretty sure your problem is that default row, I'm assuming its unindexed. If thats the case I think the value from it will overwrite the entire array and you will then lose all of the other values. Like I said, if you want a blank row to appear, you either need to put in an extra element in you list in the action class or you need to have your javascript add it when the page loads.

I'm not sure why your add and delete don't work, but thier a good chance that your javascript will need to change to deal with the JSP changes. When it creates new rows it needs to put an index in the name. Try to track down where the js is going wrong, if you need help correcting it, post the js again with details on where its going wrong.

I do this kind of stuff all the time (move data from a list to an html page and back from the page to the list), the biggest difference between the way I do it and the way you are trying to do it is in my JSP I always reference individual elements in my list (using indexes) ratherer than referencing the full array.

Vighnesh Mu
Greenhorn

Joined: Jan 06, 2011
Posts: 29

Hi Tom,

Thanks again for helping me.

I haven't changed anything in the javascript,it remains same as in the first post. My confusion is do I need to have two "datatable" or only one with iterator is enough.

Can you please provide me the example of your code showing how you accomplish this kind of stuff?
Tom Rispoli
Ranch Hand

Joined: Aug 29, 2008
Posts: 349
I think you only want 1 "datatable". Not sure why you would need 2. I think I already gave you java and jsp code. for the javascript I usuaully create the input fields with document.createElement and specify a name attribute for them with the index in it. and I call appendChild to associate the input fied with the cell. This is tedious as it requires the screen lay out to be recorded in javascript, so if someone has a slicker idea than this, you may want to look into it. But this does allow me to specify the names of the fields. If you have specific questions, let me know.
Vighnesh Mu
Greenhorn

Joined: Jan 06, 2011
Posts: 29

Thanks a lot Tom for all your reply and help. Finally I implemented this requirement.

I am pasting the code thinking it might be helpful to somebody else.
POJO -

Action class -

JSP -


Thank you again for all your help.
Tom Rispoli
Ranch Hand

Joined: Aug 29, 2008
Posts: 349
Glad I could help.
Karthik Ganesan
Greenhorn

Joined: Apr 20, 2012
Posts: 1
Tom Rispoli wrote:Glad I could help.


Very useful post. Thank you. Can you please post struts.xml?. How to persist this in database, when we sumbit multiple rows concurrently?. Can you please attach only this part of module (action class, jsp and struts.xml)?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to retain automatically generated fields on pageload in struts2
 
Similar Threads
Javascript Manipulation of Struts SELECT
Nested Tag (in Form of Table ) :: not able to submit masterForm
Multiple Select box Problem
how to delete multiple browse
adding and deleting a row in html