This is a working example. You should implement your own code according to your requirement.
Add a textBox
to your page and then use an iterable component like ui:repeat
, a4j:repeat
or c:forEach
to create multiple components.
<h:form id="test">
<ui:repeat value="#{myBean.myObjects}" var="obj">
<h:inputText value="#{obj.text}" />
<h:commandButton action="#{myBean.remove(obj)}" value="Remove"/><br/>
</ui:repeat>
<br/>
<h:commandButton action="#{myBean.add()}" value="Add"/>
</h:form>
In your managed bean getMyObjects()
method should return the list to which you add items.
public class MyBean {
private List<MyObject> objs = new ArrayList<MyObject>();
public List<MyObject> getMyObjects() {
return objs;
}
public void remove(MyObject t) {
objs.remove(t);
}
public void add() {
objs.add(new MyObject());
}
public class MyObject {
private String text;
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
}