JSF content does not set properties to Backing Bean after (re-)render
I have a JSF (Mojarra 2.1.2) page (see at the bottom) with 3 "SelectOneMenu". These are the 2 type of f:ajax actions that can happen:
A. When the user changes a value on the first list, it updates the 2 other lists.
B. When the user changes a value of the second list, it updates the thirs list only.
This almost seems to work. If the user change the 1st list, the "render='input-district-id input-sub-district-id'" code will update the 2nd and 3rd list. But then when the user changes the value of the 2nd list, the setter method in the backing bean is not called. But if the user doesn't touch the 1st list, it works well. It's only if it is refreshed/rerendered from a change on the 1st list.
I can't see what's wrong in here. Thanks for helping out.
region.xhtml:
<h:panelGrid id ="region-info" columns="4" columnClasses="valigntop,valigntop,valigntop,valigntop">
<h:panelGroup>
<h:outputText value="#{yijmsg.province}" />
<h:selectOneMenu id="input-province-id" value="#{provinceBean.provinceId}">
<f:selectItems value="#{provinceBean.provinces}" var="province"
itemLabel="#{province.provinceName}" itemValue="#{province.provinceId}"/>
<f:ajax event="change" render="input-district-id input-sub-district-id" ></f:ajax>
</h:selectOneMenu>
</h:panelGroup>
<h:panelGroup >
<h:outputText value="#{yijmsg.district}" />
<h:selectOneMenu id="input-district-id" value="#{provinceBean.districtId}">
<f:selectItems value="#{provinceBean.districts}" var="district"
itemLabel="#{district.districtName}" itemValue="#{district.districtId}"/>
<f:ajax event="change"
render="input-sub-district-id" ></f:ajax>
</h:selectOneMenu>
</h:panelGroup>
<h:panelGroup >
<h:outputText value="#{yijmsg.subdistrict}" />
<h:selectOneMenu id="input-sub-district-id" value="#{provinceBean.subdistrictId}">
<f:selectItems value="#{provinceBean.subdistricts}" var="subdistrict"
itemLabel="#{subdistrict.subdistrictName}" itemValue="#{subdistrict.subdistrictId}" />
</h:selectOneMenu>
</h:panelGroup>
<h:panelGroup >
<h:outputText value="#{yijmsg.postalCode}" />
<h:inputText value="#{registerVendorBean.vendor.postalCode}" size="6"/> *
</h:panelGroup>
</h:panelGrid>
ProvinceBean.java
@ManagedBean(name = "provinceBean")
@RequestScoped
public class ProvinceBean extends AbstractBean {
@EJB
private ProvinceFacade provinceFacade;
@EJB
private SubdistrictFacade subdistrictFacade;
@EJB
private DistrictFacade districtFacade;
private String provinceId;
private List<Province> provinces;
private String districtId;
private List<District> districts;
private String subdistrictId;
private List<Subdistrict> subdistricts;
/**
* Get the id for Province.
* @return the id for Province.
*/
public String getProvinceId() {
return provinceId;
}
/**
* Set the id for Province and reset value District and Subdistrict.
* @param provinceId to set.
*/
public void setProvinceId(String provinceId) {
this.provinceId = provinceId;
}
/**
* Get the id for District.
* @return the id for District.
*/
public String getDistrictId() {
return districtId;
}
/**
* Set the id for District and reset value Subdistrict.
* @param districtId to set.
*/
public void setDistrictId(String districtId) {
this.districtId = districtId;
}
/**
* Get the id for Subdistrict.
* @return the id for Subdistrict.
*/
public String getSubdistrictId() {
return subdistrictId;
}
/**
* Set the id for Subdistrict.
* @param subdistrictId to set.
*/
public void setSubdistrictId(String subdistrictId) {
this.subdistrictId = subdistrictId;
}
/**
* Get all Province from the database.
* @return a list of {@link Province}
*/
private List<Province> fetchProvinces() {
provinces = provinceFacade.findAll();
return provinces;
}
/**
* Get all Province call fetchProvinces method.
* @return a list of {@link Province}
*/
public List<Province> getProvinces() {
if (provinces == null) {
provinces = fetchProvinces();
if (provinces != null && provinces.size() > 0) {
provinceId = provinces.get(0).getProvinceId().toString();
}
}
return provinces;
}
/**
* Get all District from the database.
* @return a list of {@link District}
*/
private List<District> fetchDistricts() {
List<District> districtList = null;
Integer tmpProvinceId = new Integer(1);
try {
tmpProvinceId = Integer.parseInt(getProvinceId());
} catch (NumberFormatException nfe) {
//Ignore
}
if (tmpProvinceId != null) {
districtList = districtFacade.getDistrictByProvinceId(tmpProvinceId);
}
return districtList;
}
/**
* Get all District call fetchDistricts method.
* @return a list of {@link District}
*/
public List<District> getDistricts() {
if (districts == null) {
districts = fetchDistricts();
if (districts != null && districts.size() > 0) {
districtId = districts.get(0).getDistrictId().toString();
}
}
return districts;
}
/**
* Get all Subdistrict from the database.
* @return a list of {@link Subdistrict}
*/
public List<Subdistrict> fetchSubdistrict() {
List<Subdistrict> subdistrictList = null;
Integer tmpDistrictId = new Integer(1);
try {
tmpDistrictId = Integer.parseInt(getDistrictId());
} catch (NumberFormatException nfe) {
//Ignore
}
if (tmpDistrictId != null) {
subdistrictList = subdistrictFacade.getSubdistrictByDistrictId(tmpDistrictId);
}
return subdistrictList;
}
/**
* Get all Subdistrict call fetchSubdistrict method.
* @return a list of {@link Subdistrict}
*/
public List<Subdistrict> getSubdistricts() {
if (subdistricts == null) {
subdistricts = fetchSubdistrict();
if (subdistricts != null && subdistricts.size() > 0) {
subdistrictId = subdistricts.get(0).getSubdistrictId().toString();
}
}
return subdistricts;
}
}