我对 selectOneMenu 组件有疑问,我试图在 selectOneMenu 的更改事件中调用方法但不幸的是出了点问题。
当我尝试从 ajax listener 调用方法 actualizaDistritosNaturalidade 时,不会调用该方法。
它只在我选择 noSelectionOption 时调用该方法。
当我看到HTTP响应时,我看到以下错误:
<extension ln="primefaces" type="args">{"validationFailed":true}</extension>
我尝试下面的方法,甚至使用转换器波纹管。
我有以下模板文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:p="http://primefaces.org/ui">
<h:head>
<h:outputStylesheet name="style.css" library="default" />
</h:head>
<h:body>
<div id="page">
<div id="header">
<ui:insert name="header">
<h:form>
<p:menubar id="menu">
<p:menuitem>
<h:link outcome="/default.xhmtl">
<p:outputLabel value="FuncionariosGest" />
</h:link>
</p:menuitem>
<p:submenu label="Colaborador" icon="ui-icon-document">
<p:menuitem value="Criar Colaborador"
url="/create-colaborator/create-colaborator.xhtml" />
</p:submenu>
</p:menubar>
</h:form>
</ui:insert>
</div>
<div id="contentWrap" class="ui-fluid">
<div id="content">
<ui:insert name="content"></ui:insert>
</div>
</div>
<div id="footer">
<ui:insert name="footer">
bla bla
</ui:insert>
</div>
</div>
</h:body>
</html>
以下观点:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:p="http://primefaces.org/ui">
<ui:composition template="/template.xhtml">
<ui:define name="content">
<h:form>
<h:messages id="messages" />
<div class="ui-g-2">
<p:outputLabel for="birthProvince" value="Provincia :" />
</div>
<div class="ui-g-4">
<p:selectOneMenu id="birthProvince"
value="#{criarProfessionalController.profissional.provinciaDeNaturalidade}">
<p:ajax event="change" process="@this"
listener="#{criarProfessionalController.actualizaDistritosNaturalidade}" />
<f:selectItem itemLabel="Selecione uma opção" itemValue="#{null}"
noSelectionOption="true" />
<f:selectItems
value="#{criarProfessionalController.provinciasItem}" />
</p:selectOneMenu>
</div>
</h:form>
</ui:define>
</ui:composition>
</html>
在我的Managed bean中,我有以下方法(删除其他部分):
@PostConstruct
public void init() {
this.profissional = new Professional();
this.setTipoDeDocumentos(tipoDocumentoDAO.buscaTodos());
this.setProvincias(provinciaDAO.buscaTodos());
this.fillProvinciasItem();
}
public void actualizaDistritosNaturalidade() {
if (this.profissional.getProvinciaDeNaturalidade() != null) {
this.setDistritosNaturalidade(
this.distritoDAO.buscaTodosPorProvincia(this.profissional.getProvinciaDeNaturalidade()));
}
}
private void fillProvinciasItem()
{
this.setProvinciasItem(new ArrayList<SelectItem>());
for(Provincia provincia : this.getProvincias())
{
this.getProvinciasItem().add(new SelectItem(provincia, provincia.getNome()));
}
}
转换器:
@Override
public Object getAsObject(FacesContext ctx, UIComponent arg1, String provinceID) {
try {
Long id = Long.valueOf(provinceID);
ValueExpression vex = ctx.getApplication().getExpressionFactory().createValueExpression(ctx.getELContext(),
"#{criarProfessionalController}", CriarProfessionalController.class);
CriarProfessionalController profissionalController = (CriarProfessionalController) vex
.getValue(ctx.getELContext());
return profissionalController.getProvinciaByID(id);
} catch (NumberFormatException e) {
}
return null;
}
@Override
public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2) {
if (arg2 != null) {
return arg2.toString();
}
return null;
}