0
votes

Hi I have View scoped bean that is responsible for search activity. After user enters search criteria and search value, hits the search button it does populate its one of the List type properties with search results then navigate to another page where its search results are shown in data table that is bound to list that is about to contain search result. But whenever user search and reload new page to show search result, new View scoped bean is initialized. Therefore it can not show data table with expected search results. I have read few questions and articles about that and it is known as because view scoped bean is initialized every request to new page. How to resolve this issue. I bound that result list table to beans(view scoped) List property because in that list I want search results.

1

1 Answers

1
votes

You're performing search by POST which is not idempotent/bookmarkable. Do like as Google, use GET instead of POST. The <f:viewParam> is very helpful in this. Here's a kickoff example:

<f:metadata>
    <f:viewParam id="query" name="query" value="#{bean.query}" />
    <f:event type="preRenderView" listener="#{bean.search}" />
</f:metadata>
...
<form>
    <label for="query">Query</label>
    <input type="text" name="query" value="#{param.query}" />
    <input type="submit" value="Search" />
    <h:message for="query" />
</form>
...
<h:dataTable value="#{bean.results}" var="result" rendered="#{not empty bean.results}">
     ...
</h:dataTable>

With basically this @RequestScoped bean:

private String query;
private List<Result> results;

public void search() {
    results = service.search(query);
}

When the form is submitted, you end up getting the search query string in URL, making it idempotent/bookmarkable. Refreshing the request would end up in exactly the same result.