aspose file tools*
The moose likes Servlets and the fly likes Multiple forms with Use bean. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Multiple forms with Use bean." Watch "Multiple forms with Use bean." New topic
Author

Multiple forms with Use bean.

John Bateman
Ranch Hand

Joined: Mar 09, 2000
Posts: 320
Hi
I have a .jsp page that has forms for entering information. Let's say this page has forms to enter X Users into a database. on a website.
In my HTML code I can seperate each FORM by giving them a name, but the problem is when I submit this page to another .jsp where I want to do <jsp:useBean name="someUsers"/> and <jsp:setProperty name="someUsers" property="*"/>
Will the setProperty tag create an Array of Users so that each form is one user? If not, CAN I setup the useBean to make an array of Users?
If my original page had only 1 form and I did the useBean and setProperty it would populate the bean with the user information, but I would really like to be able to process multiple users at once.
Oh yeah, the page with the multiple user forms can have an undetermined number of User forms. I can also do something like pass a value of 'total forms' and then loop through them populating User objects but this is messy and also doesn't allow me to seperate my business logic from the display. I would much rather setProperty and then pass my array/collection of Users to my servlet for processing.
Any help would be very appreciated.
Thanks.

SOURCE CODE should be SURROUNDED by "code" tags.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12683
    
    5
If I understand what you are trying to do - it isn't possible. Only one <form> .. </form> at a time can be submitted in a request.
"Will the setProperty tag create an Array of Users so that each form is one user? If not, CAN I setup the useBean to make an array of Users?"
The setProperty tag is going to be calling methods based on the names of the properties with the data for the properties. It will not build an array for you.
I think you are trying to do too much in one Bean - how about making a Bean correspond to a single user, then have it check for legal data from the form properties. If all is well, pass that Bean to another class responsible for keeping the collection of users.
Bill

------------------
author of:


Java Resources at www.wbrogden.com
John Bateman
Ranch Hand

Joined: Mar 09, 2000
Posts: 320

I think you are trying to do too much in one Bean - how about making a Bean correspond to a single user, then have it check for legal data from the form properties. If all is well, pass that Bean to another class responsible for keeping the collection of users.

Hi
Thanks for the response. As an aside I think your exam cram book is great, I'd love to comment on the others but it's the only one I've got.
I guess I mislead you with my example, the real reason I am trying to process multiple forms with useBean is because if I have a User object that contains multiple Lottery Tickets, get populated in one command. (setProperty).
I guess I'll have to stop trying to be so lazy and do it the long way.
Thank again.
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
So why are these separate forms? Surely if you put all your fields into one form, and give the fields names which can be resolved to which set of data they belong to (like <input name='form1_field3'>, for example) then you can submit it and process it in one go.
If you still can't do this, you can try using JavaScript to make one button submit more than one form, although each form submission will be a separate HTTP request to the server (look up form.submit() in any JavaScript reference).


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Bhupinder Dhillon
Ranch Hand

Joined: Oct 12, 2000
Posts: 124
Originally posted by William Brogden:
If I understand what you are trying to do - it isn't possible. Only one <form> .. </form> at a time can be submitted in a request.

True, but I still think it is possible. Here is how..
Instead of creating multiple forms, you can create a multi select box for each field. Then you can use javascript to add or remove a user from those select boxes. When it's time to submit.. select everything in all the select boxes and then submit. On the server side you can access a String[] for each of the select box in the same order as you entered your user values.
<pre>
attribute user_id:
user_id1
user_id2
attribute passwd:
passwd1 //relates to user_id1
passwd2 //relates to user_id2
</pre>
and so on...
But, in order for this to work properly... you have to make sure all fields are mandatory and you have to declare two setter/getter methods for each member. One for a single value form submission...
setUserId(String id){...}
And one for multivalue...
setUserId(String[] ids){...}

BTW, I have never done this before but the logic seems ok.. it should work!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Multiple forms with Use bean.
 
Similar Threads
Strange problem in retrieving the table data in a jsp
Loading multiple forms in single jsp
Newbie help - forms and javabeans
usebean getProperty and setProperty in the same form field
Struts with Javascript problem