1
votes

I have following jsf page, in which datatable is populated via ajax request from database. Problem is, after pushing another page button on pagination bar, table shows no results. Changing scope of backing bean to session helps, but it is not a solution. Why it is happening?

    <h:form>
            <h:panelGrid columns="2" cellpadding="8" style="width: 545px">          
                <h:panelGroup>
                    <p:outputLabel value="Client name: " for="searchString" />
                    <br />                  
                    <p:inputText id="searchString" title="searchString" value="#{findClientBean.searchString}" />                       
                </h:panelGroup>
                <h:panelGroup>
                    <br />
                    <p:message for="searchString" />
                </h:panelGroup>             
                    <p:commandButton value="Search" styleClass="pCommandButton" >
                    <f:ajax execute="searchString" listener="#{findClientBean.findClient}" render=":resultTable" />
                    </p:commandButton>                                  
            </h:panelGrid>                  
            </h:form>
            <br />
            <p:dataTable id="resultTable" var="client" value="#{findClientBean.resultList}" paginator="true" rows="10"  
                 paginatorTemplate="{CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"  
                 rowsPerPageTemplate="5,10,15" paginatorPosition="bottom">
                <p:column headerText="Search results">
                    <h:outputLink value="../temp.xhtml?id=#{client.id}">#{client.firstName} #{client.lastName}</h:outputLink>
                </p:column>
            </p:dataTable>
        <h:form>

Backing bean code:

@Named
@RequestScoped
public class FindClientBean implements Serializable {

@Inject
private ClientDAO clientDAO;    
@NotNull(message="Search string cannot be empty")   
private String searchString;
private List<Client> resultList;        

public void findClient() {
    resultList = clientDAO.findClientByNameOrLastnamePart(searchString);
}

public void setResultList(List<Client> resultList) {
    this.resultList = resultList;
}

public List<Client> getResultList() {
    return resultList;
}

public String getSearchString() {
    return searchString;
}

public void setSearchString(String searchString) {
    this.searchString = searchString;
}
}
1

1 Answers

2
votes

Your Client resultList is being populated whenever the user presses the Search button.

As the collection is inside a RequestScoped bean, the resultList will be erased as soon as it is sent back to the View (along with the entire bean).

As a result, when the user tries to navigate to another page (thus making a second request) the component won't find a populated resultList anymore and a "No records found" message will be displayed.

"Promote" you bean to ViewScoped (or any scope that would make your bean live longer).

@Named
@ViewScoped
public class FindClientBean implements Serializable{
(...)
}