Search code examples
jsfwebserverjava-ee-6

Is there a way to have a JSF page automatically update a webpage when bean values change?


(Using JEE6) Is it possible to have a webpage automatically update (or listen) to values from within a bean/class and display them on the JSF when these changes happen?


Solution

  • As KayKay mentioned you can implement some sort of polling methodology using javascript to ask the server periodically to send updates if there are any. And unless you use ajax you will have to be content with only complete page refreshes.

    JSF as good as it is, sits on top of basic stateless web technology. As such unless you use Ajax or some custom code the server will only respond to a request from the client. Some libraries like icefaces have incorporated a "push" component that allows what you are looking for (from what I understand, this is a fundamental part of icefaxes). That is, to push server side changes to the client.

    You have to set up a listener on your end so that your bean will be notified when a value change happens on the server (like in your backing bean which is on the server). When the change happens you can ask say, 'icefaces push' (or another library like primefaces, which you indicate you don't want to use) to send a notice to the client. The client side code (usually ajax/javascript) will process the notice and then send a request for the whole object per normal request response. That is the notice tells the client something it's interested in changed so the client can ask for an update. Aside from the notice, still request/response.

    I mention icefaces push because it seems to be the favoured library for this now. But others have this as well. I don't believe the standard JSF 2.0 AJAX libraries have this.

    Here are a couple of resources to look at:

    (The video is a good start to get the idea of what is going on, then use the rest of the site) http://www.icesoft.org/demos/icepush-demos.jsf

    Older but I think still relevant IBM tutorial on what you want to do, using inventory changes as an example:

    http://www.ibm.com/developerworks/web/library/wa-aj-dynamic/index.html

    And another stack question related:

    Is there a better Ajax Push for JSF 2.0 than Icefaces

    Unfortunately it looks like you cannot do this with just JSF, you will have to use one of these libraries or even harder, roll your own push mechanism.