You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by JOEL VOGT <jo...@studentmail.newcastle.edu.au> on 2001/03/24 12:01:07 UTC
form question
Hi all,
I have a html form on a jsp page. This form has a table generated by the
logic iterate tag.
In each table row, there are two fields, one a checkbox and the other a
text field.
What I want is when the user clicks 'submit' all the values are sent to
an action form for storing and then to my servlet for processing.
How do I make a form that will automatically be populated by all these
values? In other words I'm stuffed please help ;)
Thanks, Joel.
Sample jsp:
<html:form action="/formAction.do">
<table border="1" width="100%">
// some headings for the table
<logic:iterate name="Bean" property="BeanItems" id="bean">
<tr>
<td>
<html:text name="bean" property="amount"/>
</td>
<td>
$<bean:write name="bean" property="total" filter="true" />
</td>
<td>
<html:checkbox name="bean" property="checkbox"/>
</td>
<td>
</tr>
</logic:iterate>
</table>
<html:submit/>
</html:form>
How do I get these values into a form then servlet then database?
Re: form question
Posted by Uwe Pleyer <uw...@up-soft.de>.
Hey,
I had some hard nights fighting with exactly this problem and got a solution
wich is not very elegant but works...
<html:form action="/formAction.do">
<table border="1" width="100%">
// some headings for the table
<% int i = 0; %>
<logic:iterate name="Bean" property="beanItems" id="anything">
<tr>
<td>
<html:text name="Bean" property='<%= "beanItems[" + i + "].amount"
%>'/>
</td>
<td>
$<bean:write name="Bean" property='<%= "beanItems[" + i + "].total"
%>' filter="true" />
</td>
<td>
<html:checkbox name="Bean" property='<%= "beanItems[" + i +
"].checkbox" %>'/>
</td>
<td>
</tr>
<% i++; %>
</logic:iterate>
</table>
<html:submit/>
</html:form>
As you see, you use the collektion of your formbean directly. The Bean needs
properties, getter and setter Methods according to the Javabean-Spec whats
the reason to change BeanItems to beanItems!
private Collection beanItems;
public Item getBeanItems(int index)
public void setBeanItems(int index, Item item)
public Item[] getBeanItems()
public void setBeanItems(Item[] items)
It's also important to configure your formbean with scope="session" in
Struts-config and never to destroy your Collection in the reset-Method! The
HTML for the input field will show like this: <input type="text"
name="beanItems[0].amount" value="123">
After submit Struts uses the following setter Method:
Bean.getBeanItems(0).setAmount("123"); As struts use the getBeanItems(i)
Method to get yor Item-Objects, there is no chance to create them in the
request-scope. There must be held from the moment you establish your
beanItems-Collection for output till the submit of the user in
session-scope.
Hope that helps
Uwe
JOEL VOGT schrieb:
> Hi all,
>
> I have a html form on a jsp page. This form has a table generated by the
> logic iterate tag.
> In each table row, there are two fields, one a checkbox and the other a
> text field.
> What I want is when the user clicks 'submit' all the values are sent to
> an action form for storing and then to my servlet for processing.
> How do I make a form that will automatically be populated by all these
> values? In other words I'm stuffed please help ;)
>
> Thanks, Joel.
>
> Sample jsp:
>
> <html:form action="/formAction.do">
> <table border="1" width="100%">
> // some headings for the table
> <logic:iterate name="Bean" property="BeanItems" id="bean">
> <tr>
> <td>
> <html:text name="bean" property="amount"/>
> </td>
> <td>
> $<bean:write name="bean" property="total" filter="true" />
> </td>
> <td>
> <html:checkbox name="bean" property="checkbox"/>
> </td>
> <td>
> </tr>
> </logic:iterate>
> </table>
> <html:submit/>
> </html:form>
>
> How do I get these values into a form then servlet then database?