I have a problem in JSF 2.1: an input text field is bound to a list in a backing bean, when submit the form, if input value is not blank, the list can fetch the new input value; if input value is blank, the list just keep old input value.
Please could anyone tell what the problem is? How to make the list in a backing bean fetch new input value exactly ?
JSF code:
<h:form id="the-form">
<ui:repeat value="#{myBean.listDate}" var="a" varStatus="aStatus">
<h:outputLabel value="input date" />
<h:inputText value="#{a}" valueChangeListener="#{myBean.onChangeCTDate}" >
<f:ajax event="change" execute="@this" listener="#{myBean.onChangeCTDate}" />
</h:inputText>
</ui:repeat>
<h:commandButton id="btnValidate" value="validate" action="#{myBean.onValidate}">
<f:ajax execute="@form" render="@form" />
</h:commandButton>
</h:form>
Bean code:
@ManagedBean(name = "myBean")
@SessionScoped
public class MyBean {
public void onChangeCTDate(ValueChangeEvent vce) throws SQLException {
String s = (String) vce.getNewValue();
System.out.println("new Value = " + s);
}
public void onChangeCTDate(AjaxBehaviorEvent e) throws SQLException {
Object val = ((UIInput) e.getSource()).getValue();
System.out.println("Value = " + val);
}
public void onValidate() {
System.out.println("---list begin---");
for (String item : listDate) {
if (item == null) {
System.out.println("Value = " + item);
} else {
System.out.println("Value = " + item);
}
}
System.out.println("---list end---");
}
}
Here is an example of runtime situation:
Open the above page,
click button "validate",
input value then click button "validate",
clear input value then click button "validate".
Runtime console display:
---list begin---
Value =
---list end---
new Value = 07/04/2021
Value = 07/04/2021
---list begin---
Value = 07/04/2021
---list end---
new Value = null
Value = 07/04/2021
new Value = null
---list begin---
Value = 07/04/2021
---list end---