What I need to do is the following, I don't know why but if a data is updated in the database it doesn't show it at once in my view, I have to run my project again to see the update of the data. I would like to make a method that checks the database every 5 seconds and updates the data I want.
I have been told that I can do it with f:ajax but I don't know how to use it, if you could give me an example to guide me I would greatly appreciate it.
The database is MySQL, I use JavaServerFaces and the connection is made through JPA.
I give you an example of what the ajax code would look like:
I'll explain, for ajax to work, you always have to create an XMLHttpRequest type object.
The essential thing is that in the.open object, you enter if it is a POST or GET type and the URL of your PHP.
The object.onload function is executed when the PHP code has been executed. The .responseText method collects the PHP response text, and then you insert it wherever you want, in my example I leave it ready for you to put it in a field specified by an id. In the document.getElementById("divotarget"), change the divotarget to the id of your element where you want to reload the information.
Finally, in object.send(), the fields that you want to send are passed to simulate sending a form to PHP, in the format "data1=data&data2=data2".
If you want this to run every 5 seconds, you put the code in a setInterval, like so.
I hope I have explained myself well. All the best.
The strategy explained in @PabloPéres-Aradros answer is a solution known as polling. That is, the client consults the server from time to time. You must use
setInterval
for a method to execute in a time interval. It happens that JSF, by default, places certain parameters in the client to ensure the state of the view and thus avoid CSRF attacks , so using the method that Pablo explains is not "so simple" when using JSF when doing everything manually. However, JSF does support polling.If you're using pure JSF, it would be best to delegate the action to a component with
<f:ajax>
:And create a JavaScript function:
If you use a library like PrimeFaces, consider that it already provides its own component to poll (code adapted from the PrimeFaces examples page ).
Consider this as a simple example. Your server-side method can execute the necessary database calls. Also, you should measure the response time of the server and the refresh of the page to place the value of your interval.
There is another strategy called push, which consists of the server executing the operations in time intervals and notifying the client that it must be updated. BalusC (Java and JSF guru) explains how to use push in JSF in this answer , partially translated:
For your problem description, in my opinion, I would use polling. In any case, it is up to you to evaluate the alternatives and choose the most convenient one to use.
For that you have two options, one is that just a moment before your query returns the result of the command, a select command is executed where everything that has that table is called again. The other is the same way but using what with timers so that every so often the statement is executed again.