Andrew
Andrew

Reputation: 39

Dynamic page update using ftl template and Spring Boot

Me question related to using Spring Boot with FTL (Freemarker) templates. Is there any way to update my page dynamically? For example: I have one general list for two accounts. When the second user change that list, I want to see those changes on my page without page reloading. Is there any way to realize that? I know that React has this possibility, but I have a lot of code with ftl templates. Will be glad for any answer

Upvotes: 0

Views: 1189

Answers (2)

Cortex
Cortex

Reputation: 684

without page reloading

That's going to be Ajax operation (if you're after the basic solution).

  • Implement jQuery on change function that watched menu select option change
  • In the on change write AJAX $.post to your service/controller, get the response value or JSON object and apply it to page using javascript ot jQuery commands.

Ajax and jQuery works nicely inside ftl templates.

Please provide more context and how your data is being served to page, either myself or another member could be able to provide a code sample.

Upvotes: 0

Kamil Bęben
Kamil Bęben

Reputation: 1172

FreeMarker generates static HTML output and nothing more.

In a website the only way to do what you are asking for (change page content without reloading) is to use JavaScript (React also uses JavaScript underneath).

You could detect when someone changed something (eg user list) using WebSocket, long-polling, etc.

Spring guide on WebSockets

Baeldung's intro to WebSocket with Spring

Ps.: You will probably have to repeat some printing logic using JavaScript, for example

  1. User A opens the view
  2. User B opens the view
  3. User B edits something
  4. On the server-side, you broadcast new WebSocket message, to inform every connected user that something has changed (could be detailed, like user by ID 123 login has changed from "donald" to "dolan")
  5. On the client-side, you listen for the broadcasted message. When you get the message described in 4, you find the table row of user with id 123, and change his login to dolan.

Upvotes: 2

Related Questions