You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@myfaces.apache.org by "Malik, Usman" <UM...@RGIS.com> on 2008/06/04 17:16:29 UTC

Tying a JSF control to a dynamic list element in backing bean

Hi All,

I am trying to put a bunch of user input controls on a web page
dynamically using tr:iterator ... and then tie each control to an
element of a dynamic list in my backing bean so I can access to the user
input from the bean. What I want to know is whether it is even possible
to set value for a specific list element from the JSP page? I can't
figure out what kind of a getter/setter method my dynamic list needs to
have.

Here is the JSP code snippet that I am thinking of ...

<tr:iterator value="#{myBean.myItemList}" var="myItem" varStatus="rows">
    <h:inputText binding="myBean.myDynamicList[#{rows.index}]"/>
</tr:iterator>

The above inputText binding should translate into
"myBean.myDynamicList[0]", at least that's what I think should happen.
However, for this to work, I would need to have a getter/setter method
for each element of myDynamicList in the backing bean. Since
myDynamicList is supposed to be dynamic (its size can change from one
request to another), I don't know the number of elements in it and
therefore I cannot hard-code the getter/setter methods. Any suggestions
or comments would be greatly appreciated.

Thanks!

Usman Malik


NOTE: The information in this email may be confidential and legally
privileged. If you are not the intended recipient, you must not
read, use or disseminate the information; please advise the sender
immediately by reply email and delete this message and any
attachments without retaining a copy. Although this email and any
attachments are believed to be free of any virus or other defect
that may affect any computer system into which it is received and
opened, it is the responsibility of the recipient to ensure that it
is virus free and no responsibility is accepted by RGIS, LLC for
any loss or damage arising in any way from its use.

Re: Tying a JSF control to a dynamic list element in backing bean

Posted by Andrew Robinson <an...@gmail.com>.
> <tr:iterator value="#{myBean.myDynamicList}" var="myItem" varStatus="rows">
>   <h:inputText binding="#{myItem}" />
> </tr:iterator>

That isn't valid code. There is only one component per tag (barring
usage of the evil c:forEach), so you can't re-bind a component in an
iterator. You can use c:forEach to do this though. It would insert the
multiple components into the tree. You also can use binding on the
parent component and append the children programmatically
(parent.getChildren().addAll(your input text components))

-Andrew

RE: Tying a JSF control to a dynamic list element in backing bean

Posted by "Malik, Usman" <UM...@RGIS.com>.
My apologies, that was a bad job of cutting/pasting in the email, that's
why the EL looked invalid. Let me try to better explain what is
happening. I am able to access a backing bean list element by using the
VALUE property of a tag in my JSP, like this ...

<tr:iterator value="#{myBean.myDynamicList}" var="myItem"
varStatus="rows">
  <h:outputLabel value="#{myBean.myDynamicList[rows.index].value}" />
</tr:iterator>

myDynamicList is a list of type HtmlInputText in the backing bean. I
have initialized each element in myDynamicList with some text and the
JSP page will properly show that text when accessed with the statement
above. I have NOT explicitly defined a getter/setter for each element in
myDynamicList, only defined getter/setter that operate on the entire
list. Somehow the individual elements of myDynamicList are available to
the JSP page to display correctly! So far so good. However, the problem
occurs when I try to bind to that same myDynamicList by using the
BINDING property of a tag in my JSP, like this ...

<tr:iterator value="#{myBean.myDynamicList}" var="myItem"
varStatus="rows">
  <h:inputText binding="#{myItem}" />
</tr:iterator>

The above piece of code neither gets nor sets the elements of
myDynamicList, they never show up on the JSP page and they retain the
values they were initialized with in the backing bean. I have also tried
the below code and did not have success with it either.

<tr:iterator value="#{myBean.myDynamicList}" var="myItem"
varStatus="rows">
  <h:inputText binding="#{myBean.myDynamicList[rows.index]}" />
</tr:iterator>

- Usman Malik

-----Original Message-----
From: Andrew Robinson
Sent: Wednesday, June 04, 2008 2:18 PM
To: MyFaces Discussion
Subject: Re: Tying a JSF control to a dynamic list element in backing
bean

Your EL was invalid

 <tr:iterator value="#{myBean.myItemList}" var="myItem"
varStatus="rows">
    <h:inputText binding="#{myBean.myDynamicList[rows.index]}"/>
 </tr:iterator>

On Wed, Jun 4, 2008 at 9:16 AM, Malik, Usman <UM...@rgis.com> wrote:
> Hi All,
>
> I am trying to put a bunch of user input controls on a web page 
> dynamically using tr:iterator ... and then tie each control to an 
> element of a dynamic list in my backing bean so I can access to the
user input from the bean.
> What I want to know is whether it is even possible to set value for a 
> specific list element from the JSP page? I can't figure out what kind 
> of a getter/setter method my dynamic list needs to have.
>
> Here is the JSP code snippet that I am thinking of ...
>
> <tr:iterator value="#{myBean.myItemList}" var="myItem"
varStatus="rows">
>     <h:inputText binding="myBean.myDynamicList[#{rows.index}]"/>
> </tr:iterator>
>
> The above inputText binding should translate into 
> "myBean.myDynamicList[0]", at least that's what I think should happen.

> However, for this to work, I would need to have a getter/setter method

> for each element of myDynamicList in the backing bean. Since 
> myDynamicList is supposed to be dynamic (its size can change from one 
> request to another), I don't know the number of elements in it and 
> therefore I cannot hard-code the getter/setter methods. Any
suggestions or comments would be greatly appreciated.
>
> Thanks!
>
> Usman Malik
>
> ________________________________
> NOTE: The information in this email may be confidential and legally 
> privileged. If you are not the intended recipient, you must not read, 
> use or disseminate the information; please advise the sender 
> immediately by reply email and delete this message and any attachments
without retaining a copy.
> Although this email and any attachments are believed to be free of any

> virus or other defect that may affect any computer system into which 
> it is received and opened, it is the responsibility of the recipient 
> to ensure that it is virus free and no responsibility is accepted by 
> RGIS, LLC for any loss or damage arising in any way from its use.

Re: Tying a JSF control to a dynamic list element in backing bean

Posted by Andrew Robinson <an...@gmail.com>.
Your EL was invalid

 <tr:iterator value="#{myBean.myItemList}" var="myItem" varStatus="rows">
    <h:inputText binding="#{myBean.myDynamicList[rows.index]}"/>
 </tr:iterator>

On Wed, Jun 4, 2008 at 9:16 AM, Malik, Usman <UM...@rgis.com> wrote:
> Hi All,
>
> I am trying to put a bunch of user input controls on a web page dynamically
> using tr:iterator … and then tie each control to an element of a dynamic
> list in my backing bean so I can access to the user input from the bean.
> What I want to know is whether it is even possible to set value for a
> specific list element from the JSP page? I can't figure out what kind of a
> getter/setter method my dynamic list needs to have.
>
> Here is the JSP code snippet that I am thinking of …
>
> <tr:iterator value="#{myBean.myItemList}" var="myItem" varStatus="rows">
>     <h:inputText binding="myBean.myDynamicList[#{rows.index}]"/>
> </tr:iterator>
>
> The above inputText binding should translate into "myBean.myDynamicList[0]",
> at least that's what I think should happen. However, for this to work, I
> would need to have a getter/setter method for each element of myDynamicList
> in the backing bean. Since myDynamicList is supposed to be dynamic (its size
> can change from one request to another), I don't know the number of elements
> in it and therefore I cannot hard-code the getter/setter methods. Any
> suggestions or comments would be greatly appreciated.
>
> Thanks!
>
> Usman Malik
>
> ________________________________
> NOTE: The information in this email may be confidential and legally
> privileged. If you are not the intended recipient, you must not read, use or
> disseminate the information; please advise the sender immediately by reply
> email and delete this message and any attachments without retaining a copy.
> Although this email and any attachments are believed to be free of any virus
> or other defect that may affect any computer system into which it is
> received and opened, it is the responsibility of the recipient to ensure
> that it is virus free and no responsibility is accepted by RGIS, LLC for any
> loss or damage arising in any way from its use.