You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Muhariz Jabeer <mu...@gmail.com> on 2005/06/15 21:33:05 UTC
accessing fields within a collection's elements
hi guys,
I'm binding a java.util.Collection as the source to a component i'm
creating. What i need to do now is
get certain fields (fName, lName etc) from the element's contained within
the Collection
and attach it to TextFields which are being dynamically created by a Foreach
component.
The code in red is supposed to be invalid. So is there a way for me to
access those fields inside
the collection's elements? ie. col[index].fName?
the component's jwc file:
<component-specification class="com.stchome.pam.tapestry.components.NameSet"
allow-body="no" allow-informal-parameters="yes">
<parameter
property-name="source"
name="source"
direction="in"
required="yes"
type="java.lang.Object">
</parameter>
<component id="elements" type="Foreach">
<inherited-binding name="source" parameter-name="source"/>
</component>
<component id="fName" type="TextField">
<binding name='value' expression="components.elements.fName"></binding>
</component>
<component id="lName" type="TextField">
<binding name='value' expression="components.elements.lName"></binding>
</component>
<component id="iNetHandle" type="TextField">
<binding name='value' expression="components.elements.iNetHandle"></binding>
</component>
</component-specification>
--------------------------------
The component's html file:
<span jwcid="$content$">
<table width="100%">
<span jwcid="elements">
<tr>
<td><input type="text" jwcid="fName"/></td>
<td><input type="text" jwcid="lName"/></td>
</tr>
<tr>
<td><input type="text" jwcid="iNetHandle"/></td>
</tr>
</span>
<tr><td><input jwcid="@Submit" value="Add more names" type="Submit"
listener="ognl:listeners.anotherNameType"/></td></tr>
</table>
</span>
------------------------------------
The component's class file
import org.apache.tapestry.*;
public class NameSet extends BaseComponent
{
private Object source;
public Object getSource() {
return source;
}
public void setSource(Object source) {
this.source = source;
}
}
Re: accessing fields within a collection's elements
Posted by The Chris Method <th...@gmail.com>.
The problem is your binding values for your text values. The "
components.elements" represents a Foreach component, which has no IName
field or getIName method. You need to declare a page property that will
store the value of each item in the "source" Collection as the Foreach is
iterating through them. So you'd have something like this:
<component-specification class="com.stchome.pam.tapestry
.components.NameSet"
allow-body="no" allow-informal-parameters="yes">
<parameter
property-name="source"
name="source"
direction="in"
required="yes"
type="java.lang.Object">
</parameter>
<property-specification name="loopObject" type="com.stchome.pam.SomeObject
"/>
<component id="elements" type="Foreach">
<inherited-binding name="source" parameter-name="source"/>
<binding name="value" expression="loopObject"/>
</component>
<component id="fName" type="TextField">
<binding name='value' expression="loopObject.fName"></binding>
</component>
<component id="lName" type="TextField">
<binding name='value' expression="loopObject.lName"></binding>
</component>
<component id="iNetHandle" type="TextField">
<binding name='value' expression="loopObject.iNetHandle"></binding>
</component>
</component-specification>
Also, if the source paramater is a Collection, you might as well declare its
type as such.
-Chris
On 6/15/05, Muhariz Jabeer <mu...@gmail.com> wrote:
>
> hi guys,
>
> I'm binding a java.util.Collection as the source to a component i'm
> creating. What i need to do now is
> get certain fields (fName, lName etc) from the element's contained within
> the Collection
> and attach it to TextFields which are being dynamically created by a
> Foreach
> component.
>
> The code in red is supposed to be invalid. So is there a way for me to
> access those fields inside
> the collection's elements? ie. col[index].fName?
>
> the component's jwc file:
>
> <component-specification class="
> com.stchome.pam.tapestry.components.NameSet"
> allow-body="no" allow-informal-parameters="yes">
>
> <parameter
> property-name="source"
> name="source"
> direction="in"
> required="yes"
> type="java.lang.Object">
> </parameter>
>
> <component id="elements" type="Foreach">
> <inherited-binding name="source" parameter-name="source"/>
> </component>
>
> <component id="fName" type="TextField">
> <binding name='value' expression="components.elements.fName"></binding>
> </component>
>
> <component id="lName" type="TextField">
> <binding name='value' expression="components.elements.lName"></binding>
> </component>
>
> <component id="iNetHandle" type="TextField">
> <binding name='value' expression="components.elements.iNetHandle
> "></binding>
> </component>
>
> </component-specification>
>
> --------------------------------
>
> The component's html file:
>
> <span jwcid="$content$">
> <table width="100%">
> <span jwcid="elements">
> <tr>
> <td><input type="text" jwcid="fName"/></td>
> <td><input type="text" jwcid="lName"/></td>
> </tr>
> <tr>
> <td><input type="text" jwcid="iNetHandle"/></td>
> </tr>
> </span>
>
> <tr><td><input jwcid="@Submit" value="Add more names" type="Submit"
> listener="ognl:listeners.anotherNameType"/></td></tr>
> </table>
> </span>
>
> ------------------------------------
>
> The component's class file
>
> import org.apache.tapestry.*;
>
> public class NameSet extends BaseComponent
> {
> private Object source;
>
> public Object getSource() {
> return source;
> }
>
> public void setSource(Object source) {
> this.source = source;
> }
> }
>
>