How I should change following example that after changes values in inputText not disappears after commandButton was submitted? I’m understanding why it happens, but I don’t know how to fix it.
<h:form>
<h:selectOneMenu valueChangeListener="#{foo.selectChanges}" onchange="submit()" >
<f:selectItem itemValue="5" itemLabel="Five"/>
<f:selectItem itemValue="10" itemLabel="Ten"/>
</h:selectOneMenu>
<ui:repeat value="#{foo.repeatItems}" var="item">
<div>
<h:inputText value="#{item.value}"/>
</div>
</ui:repeat>
<h:commandButton value="test" action="#{foo.submit}">
</h:commandButton>
</h:form>
And bean:
public class FooBean {
private RepeatItem[] repeatItems;
private String value = "5";
public String getValue() {
return value;
}
public void setValue(final String value) {
this.value = value;
}
public void submit() {
}
public void selectChanges(ValueChangeEvent e) {
value = (String) e.getNewValue();
}
public RepeatItem[] getRepeatItems() {
repeatItems = new RepeatItem[Integer.parseInt(value)];
for (int i = 0; i < Integer.parseInt(value); i++) {
repeatItems[i] = new RepeatItem();
}
return repeatItems;
}
public void setRepeatItems(final RepeatItem[] repeatItems) {
this.repeatItems = repeatItems;
}
public static class RepeatItem {
private String value;
public String getValue() {
return value;
}
public void setValue(final String value) {
this.value = value;
}
}
}
You’d like to put the bean in the
viewscope to keep it alive in subsequent requests to the same view. You’d also like to prefill in thevalueChangeListenermethod instead of the getter method since a getter method is called multiple times in JSF’s lifecycle and is actually supposed to only return the data. You would also like to usef:ajaxhere to improve user experience by partial submits instead of submitting the whole page by Javascript’ssubmit(). Here’s a kickoff example:XHTML:
Bean:
Item:
See also:
@ViewScoped