使用转换器从p:autocomplete获取数据

时间:2016-08-06 23:00:03

标签: jsf jsf-2 primefaces autocomplete

我正在使用PrimeFaces 5.0。

我尝试做的是借助于1p:BD上的autocomplete1外观,返回建议并使用itemtip显示其他一些信息需要它,一切似乎都运行良好,问题是当我获取数据并选中,此数据未在属性上设置...

使用itemtip自动完成:

<p:row>
    <p:column colspan="10">
        <p:outputLabel value="Orden de Compra "/>
        <p:autoComplete id="itemTip" value="#{c_ordenCompra.encabezado_encontrado}" 
                        completeMethod="#{c_ordenCompra.completarOrdenCpra}"
                        var="orden" itemLabel="#{orden.numdocucv}" itemValue="#{orden.idencacv}" 
                        converter="themeConverter" forceSelection="true">
            <f:facet name="itemtip">
                <h:panelGrid columns="2" cellpadding="3" cellspacing="5">
                    <h:outputText value="Fecha" />
                    <h:outputText value="#{orden.fechadoctocv}" />

                    <h:outputText value="Sucursal" />
                    <h:outputText value="#{orden.tbsucursalesByFkencacvsucurentra.nomsucur}" />

                    <h:outputText value="Proveedor" />
                    <h:outputText value="#{orden.tbpersonasrelaByIdpersorelaprove.nomcompleto}" />
                </h:panelGrid>
            </f:facet>

        </p:autoComplete>
        <p:commandButton value="buscar" actionListener="#{c_ordenCompra.seleccionarorden()}"/>
    </p:column>
</p:row>

这是我尝试使用的转换器。 (我在这里做了一些测试,一切似乎都有效。)

public class converterOC implements Converter {
public Object getAsObject(FacesContext fc, UIComponent uic, String value) {
    if (value != null && value.trim().length() > 0) {
        try {
            M_encacv modelo_encacv = new M_encacv();
            Tbencacv encav_encontrado = modelo_encacv.Ordene1CompraEncacv(value);
            return encav_encontrado;
        } catch (NumberFormatException e) {
            throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Conversion Error", "Not a valid theme."));
        }
    } else {
        return null;
    }
}

public String getAsString(FacesContext fc, UIComponent uic, Object object) {
    if (object != null) {
        return String.valueOf(object);
    } else {
        return null;
    }
}

实体:

public class Tbencacv implements java.io.Serializable {

private int idencacv;
private Tbbancos tbbancos;
private Tbbodegas tbbodegasByFkencacvbodesalida;
private Tbbodegas tbbodegasByFkencacvbodeentra;
private Tbcajasvta tbcajasvta;
private Tbcajerosvta tbcajerosvta;
private Tbcias tbcias;
private Tbencainven tbencainven;
private Tbpersonasrela tbpersonasrelaByIdpersorelaprove;
private Tbpersonasrela tbpersonasrelaByIdpersorelaemple;
private Tbpersonasrela tbpersonasrelaByIdpersorelacliente;
private Tbsucursales tbsucursalesByFkencacvsucurentra;
private Tbsucursales tbsucursalesByFkencacvsucursalida;
private Tbterminoscv tbterminoscv;
private Tbtiposmovicv tbtiposmovicv;
private Tbvendecobra tbvendecobra;
private Date fechadoctocv;
private String observagralcv;
private String concepcontagralcv;
private Double valortotalcv;
private Double valorivacv;
private Double valorimpuperecv;
private Double valorexencv;
private Double valorgravacv;
private Double valornosujecv;
private Double valorfovialcv;
private Double porcedescuv;
private String procedecv;
private char cpravta;
private Double valorrentacv;
private int numdocucv;
private char autorizadocucv;
private Date fechablc;
private Character aplicalibroiva;
private Double valorefectivo;
private Double valortarjeta;
private Double valorcheque;
private Integer numcheque;
private String numreserva;
private String numbaucher;
private String numaprobacion;
private Character importacion;
private Character sujetoexcluido;
private Character retentercero;
private String direccionenviar;
private String nomclientedocu;
private Double valorcredito;
private Integer codprove;
private String nomvencob;
private Integer codcliente;
private Integer codemple;
private Character movcerrado;
private Character afectaexisten;
private Character docucvimpre;
private Character paraconsu;
private Character parainven;
private Character paracl;
private String nomuser;
private Set<Tbdetacv> tbdetacvs = new HashSet<Tbdetacv>(0);

public Tbencacv() {
}
//all setters and getters

在与自动填充相同的行中,我有一个命令按钮。假设在控制台上打印一个来自我在自动完成时设置的属性的数据,但它没有显示任何内容。

 public void seleccionarorden() {
    System.out.println(getEncabezado_encontrado().getValorivacv());
    System.out.println(Encabezado_encontrado.getValorivacv());
}

我尝试了一切,但我没有想法

what it show on the page

这是控制台显示的内容。甚至不是例外:

what the console show

2 个答案:

答案 0 :(得分:1)

感谢您的帮助..我开始工作

这就是它的工作原理

编辑:转换器

@FacesConverter("converterOC")
public class converterOC implements Converter {
    @Override
    public Object getAsObject(FacesContext fc, UIComponent uic, String value) {
        if (value != null && value.trim().length() > 0) {
            try {
                M_encacv modelo_encacv = new M_encacv();
                Tbencacv encav_encontrado = modelo_encacv.Ordene1CompraEncacv(value);
                return encav_encontrado;
            } catch (NumberFormatException e) {
                throw new ConverterException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Conversion Error", "Not a valid theme."));
            }
        } else {
            return null;
        }
    }

    @Override
    public String getAsString(FacesContext fc, UIComponent uic, Object object) {
        if(object != null) {
            return String.valueOf(((Tbencacv) object).getIdencacv());
        }
        else {
            return null;
        }
    }
}

编辑:自动填充(我使用ajax为itemselect事件更改了<p:CommandButton />

<p:column colspan="10"><p:outputLabel value="Orden de Compra "/>
    <p:autoComplete id="itemTip" value="#{c_ordenCompra.encabezado_encontrado}" 
                    completeMethod="#{c_ordenCompra.completarOrdenCpra}"
                    var="orden" itemLabel="#{orden.numdocucv}" itemValue="#{orden}" 
                    converter="converterOC" forceSelection="true">
        <f:facet name="itemtip">
            <h:panelGrid columns="2" cellpadding="3" cellspacing="5">
                <h:outputText value="Fecha" />
                <h:outputText value="#{orden.fechadoctocv}" />

                <h:outputText value="Sucursal" />
                <h:outputText value="#{orden.tbsucursalesByFkencacvsucurentra.nomsucur}" />

                <h:outputText value="Proveedor" />
                <h:outputText value="#{orden.tbpersonasrelaByIdpersorelaprove.nomcompleto}" />
            </h:panelGrid>
        </f:facet>
        <p:ajax event="itemSelect" listener="#{c_ordenCompra.seleccionarorden}" update="noc,date,acs,acb,soli,prov,ter,nreg,dtdetalles"  /> 

    </p:autoComplete>

答案 1 :(得分:0)

itemValue="#{orden.idencacv}"的属性 p:autoComplete 指的是orden Tbencacv对象的属性。你应该把它作为一个对象。

itemValue="#{orden.idencacv}"的{​​{1}}更改为p:autoComplete

参考Java EE教程中的15.10 Creating and Using a Custom Converter