Jsf 我的dataGrid组件未正确更新(PrimeFaces)

Jsf 我的dataGrid组件未正确更新(PrimeFaces),jsf,primefaces,datagrid,Jsf,Primefaces,Datagrid,我正在为我的测试页面做一个动态搜索引擎。几天前,我在这里问了一些东西来展示搜索结果,有人建议我使用PrimeFaces数据网格。我非常喜欢它如何显示一切,但现在我有另一个问题 我将一个列表加载到dataGrid,该列表将包含所有搜索结果。页面搜索和显示在第一次很好,但是,如果我进行另一次搜索,页面将显示新的结果,以及以前的结果(我不希望他这样做) 例如: 第一次测试搜索(哥伦比亚) 现在当我再次搜索时,哥伦比亚并没有消失 让我们现在试试墨西哥。哥伦比亚终于消失了,但现在阿根廷是最顽固的 我

我正在为我的测试页面做一个动态搜索引擎。几天前,我在这里问了一些东西来展示搜索结果,有人建议我使用PrimeFaces数据网格。我非常喜欢它如何显示一切,但现在我有另一个问题

我将一个列表加载到dataGrid,该列表将包含所有搜索结果。页面搜索和显示在第一次很好,但是,如果我进行另一次搜索,页面将显示新的结果,以及以前的结果(我不希望他这样做)

例如:

第一次测试搜索(哥伦比亚)

现在当我再次搜索时,哥伦比亚并没有消失

让我们现在试试墨西哥。哥伦比亚终于消失了,但现在阿根廷是最顽固的

我认为解决方案是在页面进行另一次新搜索之前,清除结果列表中的所有元素,但这不起作用

基本模板(我这里有搜索框):

<?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:ui="http://xmlns.jcp.org/jsf/facelets"
  xmlns:h="http://xmlns.jcp.org/jsf/html"
  xmlns:p="http://primefaces.org/ui"
  xmlns:f="http://xmlns.jcp.org/jsf/core">
<h:head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link href="css/blueprint/screen.css" type="text/css" rel="stylesheet" 
     media="screen, projection"/>
    <link href="css/blueprint/print.css" type="text/css" rel="stylesheet" 
      media="print"/>   
   <!--[if lt IE 8]><link rel="stylesheet" href="css/blueprint/ie.css" 
     type="text/css" media="screen, projection"><![endif]-->
    <h:outputStylesheet name="css/default.css"/>
    <h:outputScript library="js" name="calendar_es.js"/>
        <title><h:outputText value="Trip Hotel, tu hotel ideal"/></title>
</h:head>
<h:body>
  <div class="centerPage">
    <div id="top" class="span-24 Last">
        <h:form>
            <table>
                <tr>
                    <td rowspan="2"><h:graphicImage library="images" 
        name="hotel.png"/></td>
                    <td><span style="font-size: 13px;color:#113C75;font-
      weight: bold;">Fecha de Entrada:</span>&nbsp;&nbsp;&nbsp;&nbsp;
                        &nbsp;&nbsp;&nbsp;&nbsp;<p:calendar id="fechaE" 
            size="8" styleClass="calendarClass" readonly="true" locale="es" value="#
        {buscador.fechaE}"/></td>
                        <td>&nbsp;&nbsp;<span style="font-size: 
        13px;color:#113C75;font-weight: bold;">Fecha de Salida:
          </span>&nbsp;&nbsp;&nbsp;&nbsp;
                        &nbsp;&nbsp;&nbsp;&nbsp;<p:calendar id="fechaF" 
          size="8" styleClass="calendarClass" readonly="true" locale="es" value="#
            {buscador.fechaS}"/></td>
                </tr>
                 <tr>
                     <td><span style="font-size: 13px;color:#113C75;font-
                      weight: bold;">Número de personas:</span>
                         &nbsp;&nbsp;&nbsp;
                         <p:selectOneMenu id="nPersona" style="width:80px; 
                  font-size: 12px;" value="#{buscador.personas}">
                             <f:selectItem itemLabel="1" itemValue="1"/>
                             <f:selectItem itemLabel="2" itemValue="2"/>
                             <f:selectItem itemLabel="3" itemValue="3"/>
                             <f:selectItem itemLabel="4" itemValue="4"/>
                         </p:selectOneMenu>&nbsp;&nbsp;&nbsp;&nbsp;
                    </td>
                     <td>&nbsp;&nbsp;<span style="font-size: 
            13px;color:#113C75;font-weight: bold;">Localidad:</span>
                               &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                     <p:inputText id="localidad" style="height: 20px; width: 
        158px; 
                                  background-color: #F9FED3;font-size: 
        13px;" value="#{buscador.localidad}" />
                     </td>
                     <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
           >>>>>>>>>>>>>>>>>>>HERE IS THE BUTTON AND MY AJAX<<<<<<<<<<<<<<<<
                         <p:commandButton value="Buscar" class="buscar" 
   action="#{buscador.mostrarBusqueda()}">
                             <f:ajax listener="#
  {buscador.mostrarBusqueda()}" event="click" render=":form2"/>
                         </p:commandButton>   
       >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>         
                     </td>
                 </tr> 
             </table>       
        </h:form>
    </div>
    <div>
        <div id="left" class="span-8">
            <ui:insert name="left">
                <div id="menu">
                    <p:button value="Iniciar Sesion" class="sesion"/>
                    <p:button value="Hoteles" class="hotel"/>
                    <p:button value="Reservas" class="reservas"/>
                    <p:button value="Ofertas" class="ofertas"/>
                    <p:graphicImage library="images" name="vista.png"
                                    style="border: 1px solid black; margin-top:50px;margin-top:80px;"/>
                </div>
            </ui:insert>
        </div>
        <div class="span-16 Last">
            <div id="content" class="left_content">
               <ui:insert name="content">Content</ui:insert>
            </div>
        </div>
    </div>       
  </div>
</h:body>
<br/>
<footer>

</footer>
  <?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:ui="http://xmlns.jcp.org/jsf/facelets"
  xmlns:h="http://xmlns.jcp.org/jsf/html"
  xmlns:f="http://xmlns.jcp.org/jsf/core"
  xmlns:p="http://primefaces.org/ui">
<f:metadata>
    <f:viewAction action="#{buscador.cargarPortada()}"/>
</f:metadata>
<body>

    <ui:composition template="./templates/base.xhtml">

        <ui:define name="content">
            <h:form id="form2">
                <p:dataGrid var="hotel" value="#{buscador.display}" columns="3" rows="9" paginator="true" id="hoteles" paginatorTemplate="{CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink}">
                    <p:panel header="#{hotel.nombre}" style="text-align:center;width: 230px;height:139px;">
                        <h:panelGrid columns="1" style="width:100%;">
                            <p:graphicImage name="images/#{hotel.imagen}" style="width: 160px; height: 70px;"/>
                            <h:outputText value="#{hotel.localidad}"/>
                        </h:panelGrid>
                    </p:panel>
                </p:dataGrid>
            </h:form>
        </ui:define>

    </ui:composition>

</body>
</html>

恩特拉达费查:
费查·德萨利达:
Número de personas:
本地数据:
>>>>>>>>>>>>>>>>>>>这是按钮和我的AJAX>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
内容

索引页(模板客户端):

<?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:ui="http://xmlns.jcp.org/jsf/facelets"
  xmlns:h="http://xmlns.jcp.org/jsf/html"
  xmlns:p="http://primefaces.org/ui"
  xmlns:f="http://xmlns.jcp.org/jsf/core">
<h:head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link href="css/blueprint/screen.css" type="text/css" rel="stylesheet" 
     media="screen, projection"/>
    <link href="css/blueprint/print.css" type="text/css" rel="stylesheet" 
      media="print"/>   
   <!--[if lt IE 8]><link rel="stylesheet" href="css/blueprint/ie.css" 
     type="text/css" media="screen, projection"><![endif]-->
    <h:outputStylesheet name="css/default.css"/>
    <h:outputScript library="js" name="calendar_es.js"/>
        <title><h:outputText value="Trip Hotel, tu hotel ideal"/></title>
</h:head>
<h:body>
  <div class="centerPage">
    <div id="top" class="span-24 Last">
        <h:form>
            <table>
                <tr>
                    <td rowspan="2"><h:graphicImage library="images" 
        name="hotel.png"/></td>
                    <td><span style="font-size: 13px;color:#113C75;font-
      weight: bold;">Fecha de Entrada:</span>&nbsp;&nbsp;&nbsp;&nbsp;
                        &nbsp;&nbsp;&nbsp;&nbsp;<p:calendar id="fechaE" 
            size="8" styleClass="calendarClass" readonly="true" locale="es" value="#
        {buscador.fechaE}"/></td>
                        <td>&nbsp;&nbsp;<span style="font-size: 
        13px;color:#113C75;font-weight: bold;">Fecha de Salida:
          </span>&nbsp;&nbsp;&nbsp;&nbsp;
                        &nbsp;&nbsp;&nbsp;&nbsp;<p:calendar id="fechaF" 
          size="8" styleClass="calendarClass" readonly="true" locale="es" value="#
            {buscador.fechaS}"/></td>
                </tr>
                 <tr>
                     <td><span style="font-size: 13px;color:#113C75;font-
                      weight: bold;">Número de personas:</span>
                         &nbsp;&nbsp;&nbsp;
                         <p:selectOneMenu id="nPersona" style="width:80px; 
                  font-size: 12px;" value="#{buscador.personas}">
                             <f:selectItem itemLabel="1" itemValue="1"/>
                             <f:selectItem itemLabel="2" itemValue="2"/>
                             <f:selectItem itemLabel="3" itemValue="3"/>
                             <f:selectItem itemLabel="4" itemValue="4"/>
                         </p:selectOneMenu>&nbsp;&nbsp;&nbsp;&nbsp;
                    </td>
                     <td>&nbsp;&nbsp;<span style="font-size: 
            13px;color:#113C75;font-weight: bold;">Localidad:</span>
                               &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                     <p:inputText id="localidad" style="height: 20px; width: 
        158px; 
                                  background-color: #F9FED3;font-size: 
        13px;" value="#{buscador.localidad}" />
                     </td>
                     <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
           >>>>>>>>>>>>>>>>>>>HERE IS THE BUTTON AND MY AJAX<<<<<<<<<<<<<<<<
                         <p:commandButton value="Buscar" class="buscar" 
   action="#{buscador.mostrarBusqueda()}">
                             <f:ajax listener="#
  {buscador.mostrarBusqueda()}" event="click" render=":form2"/>
                         </p:commandButton>   
       >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>         
                     </td>
                 </tr> 
             </table>       
        </h:form>
    </div>
    <div>
        <div id="left" class="span-8">
            <ui:insert name="left">
                <div id="menu">
                    <p:button value="Iniciar Sesion" class="sesion"/>
                    <p:button value="Hoteles" class="hotel"/>
                    <p:button value="Reservas" class="reservas"/>
                    <p:button value="Ofertas" class="ofertas"/>
                    <p:graphicImage library="images" name="vista.png"
                                    style="border: 1px solid black; margin-top:50px;margin-top:80px;"/>
                </div>
            </ui:insert>
        </div>
        <div class="span-16 Last">
            <div id="content" class="left_content">
               <ui:insert name="content">Content</ui:insert>
            </div>
        </div>
    </div>       
  </div>
</h:body>
<br/>
<footer>

</footer>
  <?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:ui="http://xmlns.jcp.org/jsf/facelets"
  xmlns:h="http://xmlns.jcp.org/jsf/html"
  xmlns:f="http://xmlns.jcp.org/jsf/core"
  xmlns:p="http://primefaces.org/ui">
<f:metadata>
    <f:viewAction action="#{buscador.cargarPortada()}"/>
</f:metadata>
<body>

    <ui:composition template="./templates/base.xhtml">

        <ui:define name="content">
            <h:form id="form2">
                <p:dataGrid var="hotel" value="#{buscador.display}" columns="3" rows="9" paginator="true" id="hoteles" paginatorTemplate="{CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink}">
                    <p:panel header="#{hotel.nombre}" style="text-align:center;width: 230px;height:139px;">
                        <h:panelGrid columns="1" style="width:100%;">
                            <p:graphicImage name="images/#{hotel.imagen}" style="width: 160px; height: 70px;"/>
                            <h:outputText value="#{hotel.localidad}"/>
                        </h:panelGrid>
                    </p:panel>
                </p:dataGrid>
            </h:form>
        </ui:define>

    </ui:composition>

</body>
</html>

我的bean的搜索方法…

public void mostrarBusqueda(){
    displayList.clear();  <<<<< Here I clear my result List
    String parametros[] = new String[1];
    parametros[0] = localidad;
    try{
        ResultSet rs = GestorDB.getParametizada(parametros, "SELECT HOTELES.NOMBRE AS A, HOTELES.IMGHOTEL, LOCALIDADES.NOMBRE AS B FROM HOTELES INNER JOIN LOCALIDADES"
        + " ON LOCALIDADES.IDLOCALIDADES = HOTELES.LOCALIDADES_IDLOCALIDADES WHERE LOCALIDADES.NOMBRE LIKE '%' ||  ?  ||  '%'");
        while(rs.next()){
            ResultElement res = new ResultElement(rs.getString("A"), rs.getString("IMGHOTEL"), rs.getString("B"));
            displayList.add(res);
        }
    } catch (SQLException ex) {
         Logger.getLogger(Buscador.class.getName()).log(Level.SEVERE, null, ex);
     }
}
public void mostrarBusqueda(){

displayList.clear();试试。谢谢@stakahop!!成功了!!