I use SelectOneMenu, first i choose location, next building in this location then zone in building and subZone in this zone. First two select are good but when i take building choose there is no zones to show and i have error: j_idt31:selectedBld: Validation Error: Value is not valid. I don't know why;/
this is xhtml:
<h:form>
<p:panelGrid columns="2" position="center top">
<h:outputLabel for="selectedLoca" value="#{loc['location']}" />
<p:selectOneMenu value="#{caretakerCautionBean.location}" id="selectedLoca" converter="#{locationConverter}">
<f:selectItem itemLabel="" itemValue="#{null}" />
<f:selectItems value="#{caretakerCautionBean.locationList}" var="loca" itemLabel="#{loca.name}" itemValue="#{loca}"/>
<p:ajax update="selectedBld :messages" event="change" process="selectedLoca" listener="#{caretakerCautionBean.locationChanged}" />
</p:selectOneMenu>
<h:outputLabel for="selectedBld" value="#{loc['building']}" />
<p:selectOneMenu value="#{caretakerCautionBean.building}" id="selectedBld" converter="#{buildingConverter}">
<f:selectItem itemLabel="" itemValue="#{null}" />
<f:selectItems value="#{caretakerCautionBean.buildingList}" var="bld" itemLabel="#{bld.name}" itemValue="#{bld}" />
<p:ajax update="selectedZone :messages" event="change" process="selectedLoca selectedBld" listener="#{caretakerCautionBean.buildingChanged}" />
</p:selectOneMenu>
<h:outputLabel for="selectedZone" value="#{loc['zone']}" />
<p:selectOneMenu value="#{caretakerCautionBean.zone}" id="selectedZone" converter="#{zoneConverter}">
<f:selectItem itemLabel="" itemValue="#{null}" />
<f:selectItems value="#{caretakerCautionBean.zoneList}" var="zone" itemLabel="#{zone.name}" itemValue="#{zone}" />
<p:ajax update="selectedSubZone :messages" event="change" process="selectedLoca selectedBld selectedZone" listener="#{caretakerCautionBean.zoneChanged}" />
</p:selectOneMenu>
<h:outputLabel for="selectedSubZone" value="#{loc['cleaningPlanner.tableHeader.subzone']}" />
<p:selectOneMenu value="#{caretakerCautionBean.subZone}" id="selectedSubZone" converter="#{subZoneConverter}">
<f:selectItem itemLabel="" itemValue="#{null}" />
<f:selectItems value="#{caretakerCautionBean.subZoneList}" var="subZone" itemLabel="#{subZone.name}" itemValue="#{subZone}" />
<p:ajax update=":messages" event="change" process="selectedLoca selectedBld selectedZone selectedSubZone" listener="#{caretakerCautionBean.subZoneChanged}" />
</p:selectOneMenu>
<f:facet name="footer">
<p:commandButton process="@form" value="#{loc['addComment']}" actionListener="#{caretakerCautionBean.addNewPosition()}" update=":messages" oncomplete="caretakerCautionsDialog.hide()"/>
</f:facet>
</p:panelGrid>
</h:form>
Bean:
@ManagedBean
public class CaretakerCautionBean {
@ManagedProperty(value="#{caretakerCautionRepository}")
private CaretakerCautionRepository caretakerCautionRepo;
@ManagedProperty(value="#{locationRepository}")
private LocationRepository locationRepo;
@ManagedProperty(value = "#{buildingRepository}")
private BuildingRepository buildingRepo;
@ManagedProperty(value="#{zoneRepository}")
private ZoneRepository zoneRepo;
@ManagedProperty(value="#{subZoneRepository}")
private SubZoneRepository subZoneRepo;
@ManagedProperty (value="#{userRepository}")
private UserRepository userRepo;
@ManagedProperty(value="#{userService}")
private UserService userSvc;
private List<CaretakerCaution> caretakerCautionList;
private List<Location> locationList;
private List<User> userList;
private List<SubZone> subZoneList;
private List<Building> buildingList;
private List<Zone> zoneList;
private Location location;
private SubZone subZone;
private Building building;
private Zone zone;
private User user;
private String description;
private String comment;
private String actionsTaken;
private int id;
private transient Logger logger = Logger.getLogger("aplikacjaLogger");
public void locationChanged(AjaxBehaviorEvent ev) {
if(location !=null)
logger.log(Level.INFO, "locationChanged: "+location.getName());
buildingList = null;
getBuildingList();
}
public void buildingChanged(AjaxBehaviorEvent ev) {
if(building != null)
logger.log(Level.INFO, "buildingChanged: "+building.getName());
zoneList = null;
getZoneList();
}
public void zoneChanged(AjaxBehaviorEvent ev) {
if(zone != null)
logger.log(Level.INFO, "zoneChanged: "+zone.getName());
subZoneList = null;
getSubZoneList();
}
public void subZoneChanged(AjaxBehaviorEvent ev) {
if(subZone != null)
logger.log(Level.INFO, "subZoneChanged: "+subZone.getName());
}
public void addNewPosition() {
CaretakerCaution cc = new CaretakerCaution();
cc.setLocation(location);
cc.setBuilding(building);
cc.setZone(zone);
cc.setSubZone(subZone);
getCaretakerCautionRepo().save(cc);
logger.log(Level.INFO, "Dodano uwage.");
}
public BuildingRepository getBuildingRepo() {
return buildingRepo;
}
public void setBuildingRepo(BuildingRepository buildingRepo) {
this.buildingRepo = buildingRepo;
}
public ZoneRepository getZoneRepo() {
return zoneRepo;
}
public void setZoneRepo(ZoneRepository zoneRepo) {
this.zoneRepo = zoneRepo;
}
public UserRepository getUserRepo() {
return userRepo;
}
public void setUserRepo(UserRepository userRepo) {
this.userRepo = userRepo;
}
public Building getBuilding() {
return building;
}
public void setBuilding(Building building) {
this.building = building;
}
public Zone getZone() {
return zone;
}
public void setZone(Zone zone) {
this.zone = zone;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public List<User> getUserList() {
return userList;
}
public void setUserList(List<User> userList) {
this.userList = userList;
}
public List<Location> getLocationList() {
if(locationList == null)
locationList = getUserSvc().getLoggedIn().getCaretakenLocations();
return locationList;
}
public void setLocationList(List<Location> locationList) {
this.locationList = locationList;
}
public List<CaretakerCaution> getCaretakerCautionList() {
return caretakerCautionList;
}
public void setCaretakerCautionList(List<CaretakerCaution> caretakerCautionList) {
this.caretakerCautionList = caretakerCautionList;
}
public SubZoneRepository getSubZoneRepo() {
return subZoneRepo;
}
public void setSubZoneRepo(SubZoneRepository subZoneRepo) {
this.subZoneRepo = subZoneRepo;
}
public List<SubZone> getSubZoneList() {
if(subZoneList == null && zone != null)
subZoneList = getSubZoneRepo().findByZone(zone);
return subZoneList;
}
public void setSubZoneList(List<SubZone> subZoneList) {
this.subZoneList = subZoneList;
}
public SubZone getSubZone() {
return subZone;
}
public void setSubZone(SubZone subZone) {
this.subZone = subZone;
}
public LocationRepository getLocationRepo() {
return locationRepo;
}
public void setLocationRepo(LocationRepository locationRepo) {
this.locationRepo = locationRepo;
}
public Location getLocation() {
return location;
}
public void setLocation(Location location) {
this.location = location;
}
public List<Building> getBuildingList() {
if(buildingList == null && location != null)
buildingList = getBuildingRepo().findByLocation(location);
return buildingList;
}
public void setBuildingList(List<Building> buildingList) {
this.buildingList = buildingList;
}
public List<Zone> getZoneList() {
if(zoneList == null && building != null)
zoneList = getZoneRepo().findByBuilding(building);
return zoneList;
}
public void setZoneList(List<Zone> zoneList) {
this.zoneList = zoneList;
}
public CaretakerCautionRepository getCaretakerCautionRepo() {
return caretakerCautionRepo;
}
public void setCaretakerCautionRepo(
CaretakerCautionRepository caretakerCautionRepo) {
this.caretakerCautionRepo = caretakerCautionRepo;
}
public UserService getUserSvc() {
return userSvc;
}
public void setUserSvc(UserService userSvc) {
this.userSvc = userSvc;
}
public String getComment() {
return comment;
}
public void setComment(String comment) {
this.comment = comment;
}
public String getActionsTaken() {
return actionsTaken;
}
public void setActionsTaken(String actionsTaken) {
this.actionsTaken = actionsTaken;
}
}
Converter:
@Override
public Object getAsObject(FacesContext fc, UIComponent uiComp, String stringVal) {
if(stringVal == null || stringVal.trim().equals("")) return null;
else {
Integer userId = Integer.parseInt(stringVal);
for(Zone zon: this.getZonRepo().findAll())
if(zon.getId() == userId) {
return zon;
}
return null;
}
}
@Override
public String getAsString(FacesContext fc, UIComponent uiComp, Object obj) {
if(obj == null || obj.equals("")) {
return "";
} else {
try {
return String.valueOf(((Zone)obj).getId());
} catch(ClassCastException cce) {
return "";
}
}
}
public ZoneRepository getZonRepo() {
return zonRepo;
}
public void setZonRepo(ZoneRepository zonRepo) {
this.zonRepo = zonRepo;
}
}