使用转换器从p:自动完成获取数据



我使用的是PrimeFaces 5.0。

我试图做的是在BD上的1p:autocomplete1外观的帮助下,返回建议并使用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());
}

我尝试了我脑海中的一切,但我的想法都用完了

页面上显示的内容

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

控制台显示的内容

感谢您的帮助。。我让它工作

就是这样运作的

编辑:转换器

@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更改了项目选择事件的<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>

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

p:autoCompleteitemValue="#{orden.idencacv}"更改为itemValue="#{orden}"

请参阅15.10 Java EE教程中的创建和使用自定义转换器

相关内容

  • 没有找到相关文章

最新更新