Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/jsf/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jsf 如何在更改第一个selectOneMenu时加载第二个selectOneMenu?_Jsf - Fatal编程技术网

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

Jsf 如何在更改第一个selectOneMenu时加载第二个selectOneMenu?,jsf,Jsf,我有两个组件,其中一个取决于另一个组件的选择。当您选择第一个菜单组件的一个值时,第二个值会随着第一个菜单的onchange=“submit()”和valueChangeListener=“{Usuario.cmbDatos_action}”事件而改变: <h:selectOneMenu id="cmbCombo" binding="#{Usuario.cmbDatos}" value="#{Usuario.id}" onchange="submit()" valueChangeL

我有两个
组件,其中一个取决于另一个组件的选择。当您选择第一个菜单组件的一个值时,第二个值会随着第一个菜单的
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>

在方法
cmbDatos\u action()
的哪个部分,我可以将代码放入第二个菜单?

valueChangeListener应该引用一个方法,该方法使用
ValueChangeEvent
参数并返回
void

public void cmbDatos_action(ValueChangeEvent event) {
    // ...
}
但是在验证方面您会遇到麻烦,因为您基本上是使用
onchange=“submit()”
提交整个表单。您还需要将
immediate=“true”
添加到第一个菜单组件中。另请参见,以获取带有具体示例的详细说明

由于您似乎已经在使用JSF2.x,我建议您忘记这个基于JSF1.x的解决方案,直接使用JSF2.x
方法,它的编程非常简单

以下是一个基于“国家”和“城市”示例的启动示例:


比如说

@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);
    }

    // ...
}
@ManagedBean
@视域
公共类Bean{
私人名单国家;
私人国家;
私人名单城市;
私人城市;
@EJB
私有数据服务;
@施工后
公共void init(){
countries=service.getCountries();
}
公共资源(国家){
城市=服务。获取城市(国家);
}
// ...
}

谢谢您的完整性。
public void cmbDatos_action(ValueChangeEvent event) {
    // ...
}
<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>
@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);
    }

    // ...
}