Ender
Ender

Reputation: 1778

ManagedBean keeps returning same query results (Session/View Scope?)

I have the following in an index.xhtml page.

<h:form id="findForm">
        <h:panelGrid columns="2" cellpadding="5">                
            <p:outputLabel value="Name" for="name"/>
            <p:inputText id="name" value="#{finder.name}"/>

            <f:facet name="footer">
                <p:commandButton value="Find"
                action="#{finder.gotoFoundPage}"/>
            </f:facet>
        </h:panelGrid>
</h:form>

And here's a cut down version of the next page, found.xhtml.

    <p:dataTable id="myTable" var="item" 
        value="#{finder.byName}" rowKey="#{item.name}" 
        paginator="true" rows="20" 
        selection="#{finder.selectedItem}" selectionMode="single">

        <p:column headerText="Name" sortBy="#{item.name}"
                filterBy="#{item.name}" id="name">
            #{item.name}
        </p:column>
    </p:dataTable>

Both xhtml pages use the same mananged bean. When I click on the CommandButton from index.xhtml, it navigates to the found.xhtml page which also uses the Finder bean (currently ViewScoped). The problem is that the Finder bean will not get the #{finder.name} (which user inputs in index page and needs to be passed to found page) unless I make the Finder bean SessionScoped.

So if I make it SessionScoped, then the problem is that found.xhtml page keeps displaying the same values in the found page upon a new search at the man page. The same values even though the values in the database have changed. My understanding is that if I make it ViewScoped, it will perform a new query each time the page loads in order to update the displayed values. But not sure how to do this.

@EJBs({@EJB(name="ejb/MyBean1", beanInterface=IMyBean1.class),
       @EJB(name="ejb/MyBean2", beanInterface=IMyBean2.class)})
@ManagedBean(name="finder")
@ViewScoped
public class Finder implements Serializable {

    // ...

    public String gotoFoundPage() {
        return "found?faces-redirect=true";
    } 

Anyone have ideas?

Upvotes: 0

Views: 601

Answers (1)

Elias Dorneles
Elias Dorneles

Reputation: 23846

Remember that whenever you use the ?faces-redirect=true trick, the user's browser will redirect with an HTTP GET request and that causes JSF to clean any state stored in the view. So, even if you use an @ViewScoped bean, after a redirect you will get a brand new instance.

In your case, as it is a simple search, you may want to do it using view params, if the data isn't sensitive to appear in the URL. Check out this blog post from BalusC: Communication in JSF 2 for more information about this and other stuff related to passing data around JSF beans and pages.

Upvotes: 1

Related Questions