You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Anoop <an...@india.hp.com> on 2002/08/30 15:58:16 UTC
Dynamic name to the text fields
Hi All,
I am a newbie to struts and taglibs, was trying to display text fields
dynamically using "html:iterator". Below is the scenario I was
trying out.
This is the snippet from my jsp to display the text fields.
<logic:iterate id="UserDetail" name="user" property="userDetails">
<tr>
<td align="center">
<html:text name="UserDetail" property="org"/>
</td>
</tr>
</logic:iterate>
The html snippet for the above is something like:
<input type="text" name="org" value="Org1">
<input type="text" name="org" value="Org2">
But I want it to be something like:
<input type="text" name="a" value="Org1">
<input type="text" name="b" value="Org2">
Where the values("a" & "b") for the name attribute comes from an array
defined in the Form Bean. What are the changes required in my jsp snippet?
Also, if only it is possible to get an output as desired, does the FormBean
be modified to support this. As there won't be any getter/setter for "a" and
"b".
Appreciate any kind of help regarding this.
Regards and Thanks,
Anoop.
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>
Re: Dynamic name to the text fields
Posted by Ted Husted <hu...@apache.org>.
Don,
Don't hestitate to submit this through Bugzilla when you are ready.
-Ted.
Donald Ball wrote:
>On 8/30/2002 at 7:28 PM Anoop wrote:
>
>>Hi All,
>>
>> I am a newbie to struts and taglibs, was trying to display text fields
>>dynamically using "html:iterator". Below is the scenario I was
>>trying out.
>>
>
>You may be in luck, I've been recently trying to learn how to do something
>similar myself.
>
><snip/>
>
>>The html snippet for the above is something like:
>>
>> <input type="text" name="org" value="Org1">
>> <input type="text" name="org" value="Org2">
>>
>>But I want it to be something like:
>>
>> <input type="text" name="a" value="Org1">
>> <input type="text" name="b" value="Org2">
>>
>>Where the values("a" & "b") for the name attribute comes from an array
>>defined in the Form Bean. What are the changes required in my jsp snippet?
>>
>>Also, if only it is possible to get an output as desired, does the
>>
>FormBean
>
>>be modified to support this. As there won't be any getter/setter for "a"
>>and
>>"b".
>>
>>Appreciate any kind of help regarding this.
>>
>
>First, google around for "map-backed ActionForms" or similar. There's some
>stuff over on jguru that might be helpful. Also, I'm trying to write
>documentation for the struts user's guide for this, so I'd appreciate your
>comments on how (un)clear the following is (apologies for the formatting,
>my mail client sucks):
>
> <section name="4.2.3 Map-backed ActionForms"
>href="map_action_form_classes">
> <p>The DynaActionForm classes offer the ability to create ActionForm
>beans
> at initialization time, based on a list of properties enumerated in the
>struts
>configuration file. However, many HTML forms are generated dynamically at
>request-time. Their ActionForm beans' properties are not all known ahead of
>time, so we need a new approach.</p>
> <p>Struts allows you to make one (or more) of your ActionForm's
>properties
>' values a Map instead of a traditional atomic object. You can then store
>your form's dynamic fields' data in that Map. Here is an example of a
>map-backed ActionForm class:</p>
><pre>
><![CDATA[
>public FooForm extends ActionForm {
>
> private final Map values = new HashMap();
>
> public void setValue(String key, Object value) {
> values.put(key, value);
> }
>
> public Object getValue(String key) {
> return values.get(key);
> }
>
>}
>]]>
></pre>
> <p>In its corresponding JSP page, you can access objects stored in
>the values map using a special notation: <i>mapname(keyname)</i>. The
>parantheses in the bean property name serve to indicate that the bean
>property named <i>mapname</i> should be a Map, and that struts should look
>at the value stored with the key <i>keyname</i> in that Map to find the
>"real" property for <i>mapname(keyname)</i>.</p>
> <p>Here is a simple example:</p>
><pre>
><![CDATA[
><html:text property="value(foo)"/>
>]]>
></pre>
> <p>This will call the getValue() method on FooForm with a key value
>of "foo" to find the property value. To create a form with dynamic field
>names, you might do something like this:</p>
><pre>
><![CDATA[
><% for (int i=0; i<10; i++) {
> String name = "value(foo-" + i + ")";
> <html:text property="<%=name%>"/><br/>
>%>
>]]>
></pre>
> </section>
>
>Hope it helps, thanks in advance for any comments.
>
>- donald
>
>
>--
>To unsubscribe, e-mail: <ma...@jakarta.apache.org>
>For additional commands, e-mail: <ma...@jakarta.apache.org>
>
>
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>
Re: Dynamic name to the text fields
Posted by Donald Ball <db...@rhoworld.com>.
On 8/30/2002 at 7:28 PM Anoop wrote:
>Hi All,
>
> I am a newbie to struts and taglibs, was trying to display text fields
>dynamically using "html:iterator". Below is the scenario I was
>trying out.
You may be in luck, I've been recently trying to learn how to do something
similar myself.
<snip/>
>The html snippet for the above is something like:
>
> <input type="text" name="org" value="Org1">
> <input type="text" name="org" value="Org2">
>
>But I want it to be something like:
>
> <input type="text" name="a" value="Org1">
> <input type="text" name="b" value="Org2">
>
>Where the values("a" & "b") for the name attribute comes from an array
>defined in the Form Bean. What are the changes required in my jsp snippet?
>
>Also, if only it is possible to get an output as desired, does the
FormBean
>be modified to support this. As there won't be any getter/setter for "a"
>and
>"b".
>
>Appreciate any kind of help regarding this.
First, google around for "map-backed ActionForms" or similar. There's some
stuff over on jguru that might be helpful. Also, I'm trying to write
documentation for the struts user's guide for this, so I'd appreciate your
comments on how (un)clear the following is (apologies for the formatting,
my mail client sucks):
<section name="4.2.3 Map-backed ActionForms"
href="map_action_form_classes">
<p>The DynaActionForm classes offer the ability to create ActionForm
beans
at initialization time, based on a list of properties enumerated in the
struts
configuration file. However, many HTML forms are generated dynamically at
request-time. Their ActionForm beans' properties are not all known ahead of
time, so we need a new approach.</p>
<p>Struts allows you to make one (or more) of your ActionForm's
properties
' values a Map instead of a traditional atomic object. You can then store
your form's dynamic fields' data in that Map. Here is an example of a
map-backed ActionForm class:</p>
<pre>
<![CDATA[
public FooForm extends ActionForm {
private final Map values = new HashMap();
public void setValue(String key, Object value) {
values.put(key, value);
}
public Object getValue(String key) {
return values.get(key);
}
}
]]>
</pre>
<p>In its corresponding JSP page, you can access objects stored in
the values map using a special notation: <i>mapname(keyname)</i>. The
parantheses in the bean property name serve to indicate that the bean
property named <i>mapname</i> should be a Map, and that struts should look
at the value stored with the key <i>keyname</i> in that Map to find the
"real" property for <i>mapname(keyname)</i>.</p>
<p>Here is a simple example:</p>
<pre>
<![CDATA[
<html:text property="value(foo)"/>
]]>
</pre>
<p>This will call the getValue() method on FooForm with a key value
of "foo" to find the property value. To create a form with dynamic field
names, you might do something like this:</p>
<pre>
<![CDATA[
<% for (int i=0; i<10; i++) {
String name = "value(foo-" + i + ")";
<html:text property="<%=name%>"/><br/>
%>
]]>
</pre>
</section>
Hope it helps, thanks in advance for any comments.
- donald
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>