我有2个<h:selectOneMenu>
组件,其中一个取决于另一个的选择。当您选择第一个菜单组件的一个值时,第二个值会随着以下事件而更改onchange="submit()"
and 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>
它就像所选国家/地区的国家/地区和城市。第一个菜单加载如下:
@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;
}
}
但是当我选择第一个菜单的一个值时,我不知道如何加载下一个菜单。我已经尝试过如下:
public String cmbDatos_action() {
try {
int intValor = Integer.parseInt(cmbDatos.getValue().toString());
} catch(Exception ex) {
}
return null;
}
在方法的哪一部分cmbDatos_action()
我可以输入代码来加载第二个菜单吗?
The valueChangeListener
应该参考一种方法ValueChangeEvent
论证和返回void
.
public void cmbDatos_action(ValueChangeEvent event) {
// ...
}
但是您会在验证方面遇到麻烦,因为您基本上是在提交entire形式与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>
例如
@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);
}
// ...
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)