简体   繁体   English

如何从Back bean更新jsf数据表

[英]How to update jsf datatable from back bean

I have a form that user should enter some values to get some info from a web service. 我有一个表单,用户应输入一些值以从Web服务获取一些信息。 Firstly user fulfills the form and then as he clicks the request button webservice is called. 首先,用户填写表格,然后在单击请求按钮时调用Web服务。 Until here everything works nicely. 到这里为止,一切都很好。 But as the webservice returns the info, I have to re-render the datatable with the new data. 但是随着Web服务返回信息,我必须用新数据重新渲染数据表。 Here is my page: 这是我的页面:

<h:body>
    <h:form id="formCus">
        <h:outputLabel value="Müşteri Tipi: *"/>
        <p:selectOneMenu id="customerType" value="#{customerService.musteriTipi}" style="width: 39%">
            <f:selectItem itemLabel="" itemValue=" " />
            <f:selectItem itemLabel="Bireysel" itemValue="BIREYSEL" />
            <f:selectItem itemLabel="Tüzel" itemValue="TUZEL" />
            <f:selectItem itemLabel="Yabancı" itemValue="YABANCI" />
            <p:ajax event="change" update="#{customerService.musteriTipi}"/>
        </p:selectOneMenu>

        <h:outputLabel value="Ad/Firma Adı: *" for="customerName" />
        <p:inputText id="customerName" value="#{customerService.adFirmaAdi}" title="Müşteri adı." >
            <p:ajax event="change" update="#{customerService.adFirmaAdi}" />
        </p:inputText>

        <h:outputLabel value="Soyad/Ünvan: *" for="customerSurname" />
        <p:inputText id="customerSurname" value="#{customerService.soyadUnvan}" title="Müşteriye ait soyad/ünvan." >
            <p:ajax event="change" update="#{customerService.soyadUnvan}" />
        </p:inputText>

        <h:outputLabel value="TC Kimlik No: *"  />
        <p:inputText id="customerTC" value="#{customerService.tcKimlikNo}" title="TC Kimlik numarasını buraya girin.TC numarası sadece sayılardan oluşmalıdır." >
            <p:ajax event="change" update="#{customerService.tcKimlikNo}" partialSubmit="true" process="@this"/>
        </p:inputText>

        <h:outputLabel value="Vergi No:"  />
        <p:inputText id="customerVergi" value="#{customerService.vergiNo}" title="TC Kimlik numarasını buraya girin.TC numarası sadece sayılardan oluşmalıdır." >
            <p:ajax event="change" update="#{customerService.vergiNo}" partialSubmit="true"/>
        </p:inputText>

        <h:outputLabel value="Müdürlük Kodu: *" />
        <p:inputText id="departmantId" value="#{customerService.mudurlukKodu}" title="Müdürlük kodunu buraya girin.Müdürlük kodu sadece sayılardan oluşmalıdır." >
            <p:ajax event="change" update="#{customerService.mudurlukKodu}" partialSubmit="true"/>
        </p:inputText>

        <h:outputLabel value="Müşteri Kodu: " />
        <p:inputText id="customerId" value="#{customerService.musteriKodu}" title="Müdürlük kodunu buraya girin.Müdürlük kodu sadece sayılardan oluşmalıdır." >
            <p:ajax event="change" update="#{customerService.musteriKodu}" />
        </p:inputText>

        <h:outputLabel value="E-Posta Adresi: " />
        <p:inputText id="customerMail" value="#{customerService.mail}" title="Müşteriye ait e-mail adresini buraya girin." >
            <p:ajax event="change" update="#{customerService.mail}" partialSubmit="true"/>
        </p:inputText>

        <h:outputText value=" "/>
        <p:commandButton id="query" value="Müşteri Sorgula" actionListener="#{customerService.request}" async="true"  onsuccess="panelwv.show()">
            <f:ajax execute="@form" render=":personList" ></f:ajax> 
        </p:commandButton>
    </h:form>

    <h:panelGrid columns="5">
        <h:outputText value=""/>
        <h:outputText value=""/>
        <p:panel widgetVar="panelwv" visible="false" closable="true" header="Sorgu Yapılıyor...">
            <p:graphicImage value="/resources/images/ajaxloadingbar.gif" />  
        </p:panel>
        <h:outputText value=""/>
        <h:outputText value=""/>
    </h:panelGrid>
    <h:outputText value="Bulunan Müşterilere Ait Bilgiler:" />


    <h:form id="personList" rendered="#{not empty customerService.musteriKodu}">
        <p:dataTable value="#{customerService.customer}" var="item" id="persontable" emptyMessage="Henüz müşteri eklemediniz.">
            <p:column headerText="Müşteri/Firma ID">
                #{item.customerId}
            </p:column>
            <p:column headerText="Ad/Firma Adı">
                #{item.customerName}
            </p:column>
            <p:column headerText="Soyad/Ünvan" >
                #{item.customerSurname}
            </p:column>
            <p:column headerText="Müşteri Tipi" >
                #{item.customerType} 
            </p:column> 
            <p:column headerText="Telefon" >
                #{item.customerTel}
            </p:column>
            <p:column headerText="Adres">
                #{item.customerAddress}
            </p:column>
            <p:column headerText="E-Posta">
                #{item.customerMail}
            </p:column>
        </p:dataTable>

    </h:form>
</h:body>

And here is my back bean: 这是我的菜豆:

//some getter and setters
List<Customers> customer = new ArrayList<Customers>();

public List<Customers> getCustomer() {
    return customer;
}

public void setCustomer(List<Customers> customer) {
    this.customer = customer;
}

public String request() {

    final RequestContext context = RequestContext.getCurrentInstance();
    //System.out.println("Progress...");

          //musteriSorgula(musteriSorgulaKriter());
    new Thread(new Runnable() {
        public void run() {
            try {
                musteriKodu = String.valueOf(musteriSorgula(musteriSorgulaKriter()).getMusteriBilgisi().getMusteriKodu());
                List<TelefonBilgisi> tel_result = telefonSorgula(telefonSorgulaKriter(musteriKodu)).getMusteriTelefonListesi();
                //telefon = tel_result.getMusteriTelefonListesi().get(0).getTelefonNo();

                if (tel_result.size() > 0) {
                    for (TelefonBilgisi t : tel_result) {
                        telefon = t.getTelefonNo();
                    }
                } else {
                    telefon = "No telephone.";
                }
                List<UavtAdresBilgisi> uavt_result = uavtAdresSorgula(uavtAdresSorgulaKriter(musteriKodu)).getMusteriUavtAdresListesi();

                if (uavt_result.size() > 0) {
                    for (UavtAdresBilgisi u : uavt_result) {
                        adres = String.valueOf(u.getSehir()) + ", " + String.valueOf(u.getBucak()) + ", " + String.valueOf(u.getKasaba());
                    }
                } else {
                    adres = "No address.";
                }


                Customers cust = new Customers(musteriTipi, BigInteger.valueOf(Long.valueOf(musteriKodu)), adFirmaAdi, soyadUnvan, telefon, adres, mail, projectId);
                if (!customer.contains(cust)) {
                    customer.add(cust);
                    System.out.println("Customer has been added.");

                } else {
                    System.out.println("Customer is still in the list.");
                }
            } catch (Exception ex) {
                Logger.getLogger(CustomerService.class.getName()).log(Level.SEVERE, null, ex);
                context.execute("alert('Try again.')");
            }
        }
    }).start();

    context.execute("panelwv.close()");
    return "";
}

The back bean could connect to webservice and gether the info, I can see that in the logs. Back bean可以连接到Web服务并获取信息,我可以在日志中看到它。 In the beginning my datatable is empty. 一开始我的数据表是空的。 What I want is to show the new data as the webservice responses. 我想要的是将新数据显示为Web服务响应。 context.update("personList") doesn't work when I place it below: 当我将它放在下面时, context.update("personList")不起作用:

customer.add(cust); 

If someone could help me I would be greatly appriciated. 如果有人可以帮助我,我会很高兴的。

        <f:ajax execute="@form" render=":personList" ></f:ajax> 

进行更改为

        <f:ajax execute="@form" update="persontable" render=":personList" ></f:ajax> 

Ok so what you want to do is to force the client to update the data table from your server. 好的,所以您要做的是强制客户端从服务器更新数据表。 Take a look at comet and push technology and also (if you don't need to support older browsers) WebSocket . 看看彗星推技术,以及(如果您不需要支持旧的浏览器) WebSocket

If you google around you'll find tutorials on how to do it using JSF. 如果您四处搜寻,将会发现有关如何使用JSF进行操作的教程。 And as you are using Primefaces, not that this library has comet support: checkout p:push and atmosphere support. 而且,当您使用Primefaces时,该库并不支持Comet:检出p:pushatmosphere支持。

I would prefer using RemoteCommand component as @Ömer said. 我更喜欢使用@Ömer所说的RemoteCommand组件。

Try use it at the end of the thread(inside the thread braces) with context.execute("updater();"); 尝试使用context.execute(“ updater();”);在线程末尾(在大括号内)使用它。

如果使用RichFaces,此问题很容易解决

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM