ok, i hadn't realised you were a newb.
yes, vector is on your form... i'd use an ArrayList tho (Vector is
synchronized = slower)
eg
public ActionForward execute(ActionMapping mapping, ActionForm f,
javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response) {
MyForm form = (MyForm) f;
if ("add".equals(mapping.getParameter()) {
form.getVector().add(new Record());
} else if ("save".equals(mapping.getParameter()) {
saveRecords(form.getVector());
}
}
try searching the web for struts CRUD examples / tutorials.
Balazs Michnay wrote:
OK, we're getting closer and closer to the solution... :)
It seems that only the first options works for me, because the data are held by two
<html:select> controls that are created dynamically and are populated from the
db. So when you click on the first one, the form is submitted by a Javascript
function so that the second one can be populated with db data.
Yes, I have two different .do actions for each submit button.
Your add action adds an empty row to your vector then goes back to the page,
JSP magically draws the empty record.
OK, now I completely understand the logic, I just don't see how it can be
implemented.
I really need to know where the vector should be stored (form bean?) and how
exactly I can add an element to it (create a method in the form bean class to
add elements?).
You say, that elements should be added in the action class, but how can I
reference a bean method that performs the actual adding? I couldn't figure it
out...
Of course, when you press the submit button (not the add button) all the
previously added elements must be available...
I hope my goal is clear for you,
Thanks a lot,
MB
----- Original Message ----
From: Lance <[EMAIL PROTECTED]>
To: Struts Users Mailing List <user@struts.apache.org>
Sent: Monday, April 16, 2007 2:41:05 PM
Subject: Re: Form data to Vector
Ok, so your add button creates a new blank record on your page
Your save button posts the form to some save action which validates /
saves to the db
Two options:
1.
When you click on your "add" button you submit the form
You submit the form to /add.do instead of /save.do
Your add action adds an empty row to your vector then goes back to the
page, JSP magically draws the empty record.
When you hit save, the record is in the vector so struts can just
populate it.
Worse user experience because of serverside hit
2.
Add button calls a javascript function which uses the DOM to create a
row with the inputs in the same style as your jsp
No serverside hit for "add"
Need to use a LazyList / Factory so that when you post to /save.do, the
empty record will be created before struts populates it.
Balazs Michnay wrote:
This is how I need to do it: I
have a form with two submit buttons. One should add the content of a text field
to a Vector (which is therefore in the form bean) without leaving the page, and
the other submit button should point to another page that has access to the
Vector values.
So I have two action classes, one for each submit button. Pressing one of them
should therefore add one value to the Vector. I try to do this in my JSP,
because I don't know how to call a specific bean method from the action class...
Hope I could explain it clearly...
Thanks for you help,
MB
----- Original Message ----
From: Lance <[EMAIL PROTECTED]>
To: Struts Users Mailing List <user@struts.apache.org>
Sent: Monday, April 16, 2007 2:15:06 PM
Subject: Re: Form data to Vector
Ok, im not sure how exactly you are using it but this is how I tend to
do it.
In your load() action, you hit the db and generate some object for each
row and put them into your vector.
Your jsp loops over all of the objects in the vector and displays
<input>s for each object (row)
Sometimes, you may generate a row without a serverside call (javascript)
then post the form. In this case the set(index, object) of your vector
will cause an IndexOutOfBoundsException. For this case, I use a LazyList
and a factory which creates an empty object at the new index, then
struts can populate the object with the <input> values.
http://jakarta.apache.org/commons/collections/apidocs-COLLECTIONS_3_0/org/apache/commons/collections/list/LazyList.html
Balazs Michnay wrote:
Struts does not seem to accept a Vector-typed field as a property value. The
form simply fails to load.
I'd happily use LazyList and a Factory together instead of a Vector if it's
easy to implement...
First I wanted to get it working with Vector, then I can easily switch to a
different container.
------------------------------------
In my JSP:
<html:select property="vizsgalatEsOrvos[0]">
------------------------------------
In my bean:
private Vector vizsgalatEsOrvos;
public Vector getVizsgalatEsOrvos() {
return vizsgalatEsOrvos;
}
public void setVizsgalatEsOrvos(Vector vizsgalatEsOrvos) {
this.vizsgalatEsOrvos = vizsgalatEsOrvos;
}
------------------------------------
Instantiating my Vector in the constructor won't help...
Struts should call
myForm.getMyVector().set(0, ?)
Do I need to implement this metod? Do I need to write my own setters/getters to
do this?
How can I do this exactly?
Thanks,
MB
----- Original Message ----
From: Lance <[EMAIL PROTECTED]>
To: user@struts.apache.org
Sent: Monday, April 16, 2007 12:12:00 PM
Subject: Re: Form data to Vector
Lance wrote:
Sorry, it helps if i read your question fully.
The way to do this is to name your html form elements using in the
struts (BeanUtils) conventions
If you have a form element named "myVector[3]" then struts will call
myForm.getMyVector().set(3, ?)
It is sometimes easiest to use the struts nested tags to do this
<nested:iterate property="myVector">
<nested:text property="name" />
<nested:text property="age" />
</nested:iterate>
will produce
<input type="text" name="myVector[0].name" value="" />
<input type="text" name="myVector[0].age" value="" />
<input type="text" name="myVector[1].name" value="" />
<input type="text" name="myVector[1].age" value="" />
If you are adding elements to the vector (instead of just editing
existing vector elements), you might want to use a LazyList and a
Factory together instead of a Vector, if you do, I can give you a bit
more help if you need.
Lance.
Lance wrote:
Only way to get data into a Vector is by a scriptlet or in your action.
Struts 2's OGNL has a nice way of doing this but unfortunately with
S1 we're left with dirty scriptlets in JSPs some of the time.
JSTL has a <c:forTokens /> that sometimes comes in handy where you
provide a comma separated list of Strings and iterate through the
elements.
Balazs Michnay wrote:
Thanks for the reply.
Yes, I know how to use the <logic:iterate> to display the elements
of a Vector (or ArrayList), I just don't see how to put these values
into the Vector. I mean if I have a Vector (or an ArrayList)
property in my form bean, how do I put some of my form data into my
bean property. Do I need to modify the setters/getters? There must
be a way to do this...
And yes, you're right, one the data are in the Vector, I can use
<logic:iterate> to display them.
Thanks for your help,
Regards,
MB
----- Original Message ----
From: Martin Gainty <[EMAIL PROTECTED]>
To: Struts Users Mailing List <user@struts.apache.org>
Sent: Friday, April 13, 2007 3:45:17 PM
Subject: Re: Form data to Vector
Balazs
Take a look at
http://struts.apache.org/1.0.2/api/org/apache/struts/taglib/logic/package-summary.html
you will see this example of implementing a vector via use of struts
logic:iterate in your jsp
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%
java.util.Vector vector = new java.util.Vector();
vector.add(new Integer(12));
vector.add(new Integer(5));
%>
<logic:iterate id="myCollectionElement" collection="<%= vector %>">
Do something with myCollectionElement
</logic:iterate>
This email message and any files transmitted with it contain
confidential
information intended only for the person(s) to whom this email
message is
addressed. If you have received this email message in error, please
notify
the sender immediately by telephone or email and destroy the original
message without making a copy. Thank you.
----- Original Message ----- From: "Balazs Michnay"
<[EMAIL PROTECTED]>
To: "Struts Users Mailing List" <user@struts.apache.org>
Sent: Friday, April 13, 2007 4:09 AM
Subject: Form data to Vector
Hi,
I have a form with two submit buttons. One should add the content
of a text field to a Vector (which is in the form bean?), and the
other one should point to another page that has access to the
Vector values.
I'd like to know how to add form data to a Vector.
Any help would be appreciated.
Thanks a lot,
MB
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]