如何在更改第一个selectOneMenu时加载第二个selectOneMenu?

Fre*_*nco 2 jsf

我有2个<h:selectOneMenu>组件,其中一个取决于另一个组件的选择.当您选择的第一个菜单组件,然后用该事件的第二个变化的一个值onchange="submit()"valueChangeListener="#{Usuario.cmbDatos_action}"第一菜单:

<h:selectOneMenu id="cmbCombo" binding="#{Usuario.cmbDatos}" value="#{Usuario.id}" 
    onchange="submit()" valueChangeListener="#{Usuario.cmbDatos_action}">
    <f:selectItems value="#{beanCombos.datos}"></f:selectItems>
</h:selectOneMenu>
Run Code Online (Sandbox Code Playgroud)

它就像所选国家的国家和城市.第一个菜单加载如下:

@ManagedBean
@RequestScoped
public class BeanCombos {

    private List<SelectItem> Datos;

    public BeanCombos() {
        try {
            clsConexion objConexion = new clsConexion();
            String strSQL = "SELECT * FROM Usuarios";            
            objConexion.ResultSetSQL = objConexion.EjecutarConsulta(strSQL);
            Datos = new ArrayList<SelectItem>();

            while (objConexion.ResultSetSQL.next()) {
                Usuario objUsuario = new Usuario();                
                objUsuario.setId(String.valueOf(objConexion.ResultSetSQL.getInt("Codigo")));
                objUsuario.setNombre(objConexion.ResultSetSQL.getString("Nombres").toUpperCase());
                Datos.add(new SelectItem(objUsuario.getId(), objUsuario.getNombre()));
            }
        } catch(Exception ex) {
            String strError = ex.getMessage().toString();            
        }
    }

    public List<SelectItem> getDatos() {
        return Datos;
    }
}
Run Code Online (Sandbox Code Playgroud)

但是当我选择第一个菜单的一个值时,我不知道如何加载下一个菜单.我试过它如下:

public String cmbDatos_action() {
    try {
        int intValor = Integer.parseInt(cmbDatos.getValue().toString());
    } catch(Exception ex) {

    }

    return null;
}
Run Code Online (Sandbox Code Playgroud)

cmbDatos_action()我可以在代码的哪个部分加载第二个菜单?

Bal*_*usC 10

valueChangeListener应该是指一种方法采取ValueChangeEvent的参数和返回void.

public void cmbDatos_action(ValueChangeEvent event) {
    // ...
}
Run Code Online (Sandbox Code Playgroud)

但是你会在验证方面遇到麻烦,因为你基本上都提交了整个表格onchange="submit()".您还需要添加immediate="true"到第一个菜单组件.有关具体示例的详细说明,请参阅本文.

由于你似乎已经在使用JSF 2.x,我建议忘记这个基于JSF 1.x的解决方法,直接使用JSF 2.x <f:ajax>方法,这种方法更简单.

这是一个基于"国家"和"城市"示例的启动示例:

<h:selectOneMenu value="#{bean.country}" converter="countryConverter">
    <f:selectItems value="#{bean.countries}" var="country" itemValue="#{country}" itemLabel="#{country.name}" />
    <f:ajax listener="#{bean.changeCountry}" render="cities" />
</h:selectOneMenu>
<h:selectOneMenu id="cities" value="#{bean.city}" converter="cityConverter">
    <f:selectItems value="#{bean.cities}" var="city" itemValue="#{city}" itemLabel="#{city.name}" />
</h:selectOneMenu>
Run Code Online (Sandbox Code Playgroud)

例如

@ManagedBean
@ViewScoped
public class Bean {

    private List<Country> countries;
    private Country country;
    private List<City> cities;
    private City city;

    @EJB
    private DataService service;

    @PostConstruct
    public void init() {
        countries = service.getCountries();
    }

    public void changeCountry() {
        cities = service.getCities(country);
    }

    // ...
}
Run Code Online (Sandbox Code Playgroud)