Reputation: 3622
I have the following facelet code which is failing:
<h:form>
<rich:select defaultLabel="Seleccionar región">
<f:selectItems value="#{StaticInfo.regiones}" var="region" itemValue="#{region.reg_Cod}" itemLabel="#{region.reg_Nombre}" />
<a4j:ajax event="change" render="provs" />
</rich:select>
<rich:select id="provs" defaultLabel="Seleccionar provincia">
<f:selectItems value="#{region.provincias}" var="prov" itemValue="#{prov.prov_Cod}" itemLabel="#{prov.prov_Nombre}" />
</rich:select>
</h:form>
Backing bean:
public class StaticInfoBean {
private ArrayList<Region> regiones;
public StaticInfoBean() {
try
{
RegionDAO regDao = new RegionDAO();
regDao.prepareConnection();
ProvinciaDAO provDao = new ProvinciaDAO();
provDao.setCon(regDao.getCon());
ComunaDAO comDao = new ComunaDAO();
comDao.setCon(regDao.getCon());
regiones = regDao.listaRegiones();
for(Region r : regiones)
{
regDao.findProvincias(r);
for(Provincia p : r.getProvincias())
{
provDao.findComunas(p);
for(Comuna c : p.getComunas())
{
comDao.findColegios(c);
}
}
}
regDao.getCon().close();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
public ArrayList<Region> getRegiones() {
return regiones;
}
public void setRegiones(ArrayList<Region> regiones) {
this.regiones = regiones;
}
public String toString() {
return regiones.toString();
}
}
Class Region
:
public class Region {
private String Reg_Cod;
private String Reg_Nombre;
private ArrayList<Provincia> provincias;
//Getters and setters
The problem: The first rich:select
tag works just fine. However the second one doesn't display any value. Not just that, but I'm working in NetBeans and it doesn't display the list of methods for class Provincia
whenever I type "prov" in the EL.
Any help is appreciated.
EDIT: I edited my code and did the following:
<h:form>
<rich:select defaultLabel="Seleccionar región" value="#{StaticInfo.regionElegida}">
<f:selectItems value="#{StaticInfo.regiones}" var="region" itemValue="#{region.reg_Cod}" itemLabel="#{region.reg_Nombre}" />
<a4j:ajax event="click" render="provs" execute="@this" />
<a4j:ajax event="click" render="texto" execute="@this" />
</rich:select>
<h:outputText id="texto" value="#{StaticInfo.regionElegida.reg_Nombre}" />
<rich:select id="provs" defaultLabel="Seleccionar provincia" value="#{StaticInfo.provinciaElegida}" rendered="#{not empty StaticInfo.regionElegida}">
<f:selectItems value="#{StaticInfo.regionElegida.provincias}" var="prov" itemValue="#{prov.prov_Cod}" itemLabel="#{prov.prov_Nombre}" />
</rich:select>
</h:form>
What's surprising is that the outputText isn't being displayed! Why would this happen?
Upvotes: 0
Views: 1526
Reputation: 23796
The variable region
set in f:selectItems
is only available inside its scope.
You have to bind each rich:select
to a backing bean property.
So, you need to change your bean code, adding properties for the chosen region to the bean:
public class StaticInfoBean {
private ArrayList<Region> regiones;
private Region regionElegida;
private Provincia provinciaElegida; // <-- you'll probably want this too...
// ... getters and setters and your initialization code
// and you need something to find the real region object that
// used the reg_Cod value got from rich:select, for now you can try this:
public void updateRegionElegida(AjaxBehaviorEvent e){
// set the chosen region to regionElegida,
// or the poor man's converter
for (Region region : regiones){
if(regionElegida.getReg_Cod() == region.getReg_cod()) {
regionElegida = region;
}
}
}
}
and then change your XHTML to something like this:
<h:form>
<rich:select value="#{StaticInfo.regionElegida}"
defaultLabel="Seleccionar región">
<f:selectItems value="#{StaticInfo.regiones}" var="region"
itemValue="#{region.reg_Cod}" itemLabel="#{region.reg_Nombre}" />
<a4j:ajax event="change" render="provs"
listener="#{StaticInfo.updateRegionElegida}" />
</rich:select>
<rich:select id="provs" value="#{StaticInfo.provinciaElegida}"
defaultLabel="Seleccionar provincia">
<f:selectItems value="#{StaticInfo.regionElegida.provincias}" var="prov"
itemValue="#{prov.prov_Cod}" itemLabel="#{prov.prov_Nombre}" />
</rich:select>
</h:form>
Now, the professional way of doing that conversion thing would be to use a custom converter that can find the appropriate instance of a Region object based on the value of the expression itemValue
used for its select widget. Check out this example from another question.
Note that rich:select
only adds functionality to the default h:selectOneMenu
, you would do good checking out its info page here at SO.
Upvotes: 2