Tanooki
Tanooki

Reputation: 41

@viewScoped data bean survives after navigation to another pages (views) with Spring Web Flow and JSF-2

I'm using Spring Web Flow and JSF-2.

The fact is strange and incomprehensible to me.

I have a main page, a typical index.

And I have and actionView with some filters (filtering will be made with an ajax calling) and a table for displaying the results according to filter info.

The back bean for this action is definied as @ViewScoped. If, at first, I navigate to index after filtering some data and then, later, I return to the action view I don't expect to found the last search displayed, I expect an empty view, but the filters are not empty and data result is filtered.

Why? If I'm defining a @ViewScope is due to I expect the back bean information has to be deleted when I change the view (and index is another view in my case) but I must be making some mistake.

Here is my code:

My parent-flow (simpifying):

<view-state id="index" view="../index.xhtml" redirect="true" popup="true"/>

<view-state id="action1Flow" view="flowRedirect:action1-flow" />

<global-transitions>
    <transition on="home" to="index" />
    <transition on="action1" to="action1Flow" />
</global-transitions>

action1-flow: (start-state="action1View")

<view-state id="action1View" view="../views/action1View.xhtml" redirect="true" popup="true"/>

action1View.xhtml: (Simplifying, one filter -> table results)

...
<p:panel id="filter" header="Filter example">
    <h:panelGrid columns="2">
        <h:outputLabel for="dataFilter" value="Filter"/>
        <p:inputText id="dataFilter" value="#{action1View.dataValue}"/>
            <p:ajax event="keyup" listener="#{action1View.filterData()}" update="table"/>
        </p:inputText>
    </h:panelGrid>
   </p:panel>

  <p:panel id="display" header="Data filtered">    
        <p:dataTable id="table" var="data" value="#{action1View.resultData"
         selection="#{action1View.selectedData}" rowKey="#{data.dataValue}"> 
        <p:column headerText="#{msg.id}" sortBy="#{data.dataValue}">
            <h:outputText value="#{data.dataValue}" />
        </p:column>      
    </p:dataTable>
</p:panel>
...
<p:commandButton value="Go to index" action="home"/>
...

and action1View.java, the back bean:

@ManagedBean
@ViewScoped
@Component("action1View")
public class Action1View implements Serializable {
    static final long serialVersionUID = 42L;
    List<ExampleBean> resultData = null;
    ExampleBean selectedData = null;
    Integer dataValue; 

    public ExampleBean getSelectedData() {
        return selectedData;
    }
    public void setSelectedData(ExampleBean selectedData) {
        this.selectedData = selectedData;
    }
    public Integer getDataValue() {
        return dataValue;
    }
    public void setDataValue(Integer dataValue) {
        this.dataValue = dataValue;
    }
    public void filterData() {
        // Some logic
        resultData = xxxxx;
    }
}

index.xhtml:

...
<p:commandButton value="Go to index" action="action1"/>
...

Sorry for my english and ...

Greetings!

Upvotes: 0

Views: 598

Answers (1)

Tanooki
Tanooki

Reputation: 41

I have found the GAP!

My bean, apart from @ViewScoped annotation, is also defining (as you can see in the code) with this annotation: @Component("action1View") without apparent reason.

The fact is by removing this annotation I got everything to work properly.

I think that is because the Component behaviur overlays ViewScoped one doing that only one bean for each defined class can be created, and, becauso of this, information stays in time (until the session ends or the application was closed).

But it would be great if someone give more and richer information about it.

Upvotes: 0

Related Questions