1

Using PrimeFaces 5.10, JSF 2, and Wildfly, I am trying to get my xhtml page to interact with a single instance of a @SessionScoped bean with a PF poll component. Each time the poll calls the bean method, a new instance of the bean is created. I've tried @ViewScoped and @SessionScoped with no change in behavior. I've noticed other similar questions, but have not seen any with a solution I have been able to implement.

I know the println's are not a reliable way to show order of method calls, but i'm using them to merely demonstrate which methods are being called and it appears that the init() method gets called over and over, even though I have @PostConstruct, so it's new instantiations. I'm even printing out "this" and it's showing different hashes each time it prints.

It never gets through the if statement in refreshTweets() because the stopPolling field never gets set to false in the right context.

I've run into this problem before and have been able to work around it rather than solve it. If anyone has any ideas as to what I am doing wrong, please let me know.

Below is the relevant xhtml code:

<p:layoutUnit id="center" position="center" styleClass="dataBox">
    <h:form id="TwitterFeed">
        <p:panelGrid columns="2">
            <p:outputLabel value="Twitter topic to query: " />
            <p:inputText value="#{dataBean.tweetTopic}"/>
            <p:outputLabel value="Number of Twitter Results: " />
            <p:inputText value="#{dataBean.tweetCount}" />
            <p:commandButton value="Submit" action="#{dataBean.toggleRenderTweets}" update="tweets"/>
            <p:commandButton value="Polling" action="#{dataBean.togglePolling}" update="tweets"/>                           
        </p:panelGrid>
        <p:panel visible="#{dataBean.renderTweets}" id="tweets">
            <p:panelGrid columns="1">
                <p:dataTable id="twitterTable" value="#{dataBean.tweetList}" var="tweetStatus">
                    <p:columns value="#{dataBean.tweetColumns}" var="column" columnIndexVar="colIndex">
                        <f:facet name="header">
                            <h:outputText value="#{column.header}"/>
                        </f:facet>
                        <h:outputText value="#{tweetStatus[column.property]}"/>
                    </p:columns>
                </p:dataTable>
                <p:poll interval="10" listener="#{dataBean.refreshTweets}" update="twitterTable" widgetVar="tweetPoll" id="tweetPoll" autoStart="true"/>
            </p:panelGrid>
        </p:panel>
    </h:form>        
</p:layoutUnit>

The relevant bean code is below:

import javax.enterprise.context.SessionScoped;
import javax.faces.bean.ManagedBean;
import javax.annotation.PostConstruct;
import java.io.Serializable;

@SessionScoped
@ManagedBean
public class DataBean implements Serializable {

    private List<TwitterStatusModel> tweetList;
    private boolean renderTweets;
    private boolean stopPolling;


    @PostConstruct
    public void init() {
        <<initialize fields>>
        stopPolling = true;
        System.out.println(this + " init()");
    }

    private void getTweets() {
       <<this method sets the List above tweetList>>
    }

    public void refreshTweets() {
        if (!stopPolling) {
            <<Method never passes the if statement because stopPolling is set to true in init()
        }

    public void togglePolling() {
        stopPolling = !(stopPolling);
        System.out.println(this + "Toggling polling - now " + stopPolling);
    }
Brooks
  • 7,099
  • 6
  • 51
  • 82
  • Just to be sure (since you did not post them) but do you have the correct imports of SessionScoped etc? With a @ManagedBean you need the javax.faces.bean.* ones not the javax.enterprise.context ones or javax.faces.view – Kukeltje Jan 27 '15 at 20:58
  • Hahaha that'll teach me to type faster on my phone or use shorter comments ;-) – Kukeltje Jan 27 '15 at 20:59
  • I changed the import as suggested and it's no longer instantiating the bean multiple times. I still have a problem with the functionality of the bean, but I suspect that's a short circuit between the chair and keyboard, so....Thank you!! So, does someone want to provide the answer so I can nominate as best answer? – Brooks Jan 27 '15 at 21:54
  • Sure, and since BalusC is waaaaaaay ahead (of everyone) in posts, points and quality of the posts, I'll take the honours ;-) – Kukeltje Jan 27 '15 at 22:29

1 Answers1

2

You should use the correct imports of @SessionScoped etc. With a @ManagedBean you need the javax.faces.bean.* ones not the javax.enterprise.context ones or javax.faces.view

See also Why are there different bean management annotations

Community
  • 1
  • 1
Kukeltje
  • 12,223
  • 4
  • 24
  • 47