jsf - Values of h:inputText inside ui:repeat are not processed -
i want process form (valuechanguelistener not valid in real case).
this bean:
public class testbean extends privatebasebean implements serializable { private list<string> strings; @postconstruct public void init() { strings = new arraylist<string>(); strings.add(""); strings.add(""); strings.add(""); } public void saveaction(actionevent event) { stringbuilder texttoshowinmessage = new stringbuilder(); (string string : strings) { texttoshowinmessage.append(string); texttoshowinmessage.append("; "); } facesmessage msg = new facesmessage(super.getbundle().getstring( texttoshowinmessage.tostring()), ""); facescontext.getcurrentinstance().addmessage(null, msg); } getters... setters...
an view:
.... <h:form> <ui:repeat var="string" value="#{testbean.strings}"> <h:inputtext value="#{string}" /> <br /> </ui:repeat> <p:commandbutton value="#{msg.save}" actionlistener="#{testbean.saveaction}" icon="ui-icon-disk" update="@form" /> </h:form> ...
when form processed in bean string list blank.
how process form intput's inside iteration, without value changue listener?
there screenshots:
the same problem occurs action or actionlistener on
your problem not connected primefaces <p:commandbutton>
's behaviour, rather scoping problem implicilty created when using <ui:repeat>
tag.
first of all, let's depart example. basically, you've got
<ui:repeat value="#{bean.strings}" var="s"> <h:inputtext value="#{s}"/> </ui:repeat>
with backing list<string> strings
.
the culprit here: value="#{s}"
. exported <ui:repeat>
variable s
visible within loop , it not bound managed bean's property, instead local variable. put differently, s
not bound/equal bean.strings[index]
1 expect , has no knowledge, see, originated from. basically, you're off unilateral relationship: value bean printed in input properly, reverse not happening.
the workarounds
workaround #1: wrapper classes / model objects
the situation can overcome using wrapper object class. in case of string 'simple mutable string', below:
public class mstring { private string string;//getter+setter+constructor }
in case iteration working predicted:
<ui:repeat value="#{bean.mstrings}" var="ms"> <h:inputtext value="#{ms.string}"/> </ui:repeat>
with backing list<mstring> mstrings
.
note if have model class, user
, , change properties within <ui:repeat>
class wrapper, properties set appropriately.
workaround #2: chained property access
another workaround consists of accessing element of collection directly within <h:inputtext>
tag. way, such property set accessing bean, collection, setting property @ desired index. excessively long, that's how is. how question, <ui:repeat>
provides exported current iteration status variable, varstatus
, used access array/collection in managed bean.
in case iteration working predicted:
<ui:repeat value="#{bean.strings}" var="s" varstatus="status"> <h:inputtext value="#{bean.strings[status.index]}"/> </ui:repeat>
with ordinary backing list<string> strings
.
Comments
Post a Comment