Triggering an Ajax event when an object changes its state

I have a small university project in which I have to show how certain algorithms work. Decided to use JSF for a simple webapp and save all the data generated by the algorithm to a database.

I thought, "It would be great if I could show the user how the algorithm works step by step, nothing fancy, just formatted text that changes when the algorithm does its thing. The original Ajax textbox should do the job!" But I cannot solve my problem. Ajax fires when a certain event occurs like 'click' or 'keyup', but is there a way to get it to fire when an object in my ManagedBean class changes?

Don't know if I've explained enough, here's an example of what I want to do:

  • ArrayList in ManagedBean is empty, output text does not return anything
  • After a certain amount of time, the new object is saved in triggers ArrayList, Ajax, and the output text outputs it onString ()
  • After some time, again the same object that was printed before the changes, the Ajax triggers and the output text change accordingly.

I apologize if the problem is not entirely clear, I tried to describe it as much as possible.

+3


source to share


1 answer


What you are describing is not classic, not client-side AJAX, but known as server-dispatched events (SSE). They are usually implemented using long polling (periodic client requests that only get a response if there is a server event).

Your favorite JSF toolkit probably supports it called "Push". Here's an Example Price Lists .



However, for your simple use case, I would suggest using a poll. See this example . The main difference is that your server side logic switches to storing a list "to display" strings / ids / objects. When your client-browser is polling the server (listener method is called), assign the first item to a field String

in the fallback bean and ask your client to re-render the scope where this is displayed String

.

Note that you can do this in plain JSF-2, no need for Primefaces.

0


source







All Articles