Home > Enterprise >  Refresh data from database constantly
Refresh data from database constantly

Time:12-17

I have this Spring VMC table which I would like to display:

@Controller
public class FileImportsController {

    private EntityImportRequestsService entityImportRequestsService;

    @Autowired
    public FileImportsController(EntityImportRequestsService entityImportRequestsService) {
        this.entityImportRequestsService = entityImportRequestsService;
    }

    @GetMapping("/imported_files")
    public String viewHomePage(Model model) {
        List<EntityImportRequestsTable> listProducts = entityImportRequestsService.findAll();
        model.addAttribute("listProducts", listProducts);
        return "index";
    }

}

page:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">
<head>
  <meta charset="utf-8"/>
  <title>Product Manager</title>
</head>
<body>
<div align="center">
  <h1>Product List</h1>
  <table border="1" cellpadding="10">
    <thead>
    <tr>
      <th>Product ID</th>
      <th>Name</th>
      <th>Brand</th>
      <th>Made In</th>
      <th>Price</th>
    </tr>
    </thead>
    <tbody>
    <tr th:each="product : ${listProducts}">
      <td th:text="${product.requestId}">Product ID</td>
      <td th:text="${product.name}">Name</td>
      <td th:text="${product.brand}">Brand</td>
      <td th:text="${product.madein}">Made in</td>
      <td th:text="${product.price}">Price</td>
    </tr>
    </tbody>
  </table>
</div>
</body>
</html>

How I can pull database table data every 5 seconds and refresh the table data?

CodePudding user response:

Use the meta tag within the head element of your HTML as explained here: https://www.geeksforgeeks.org/how-to-automatic-refresh-a-web-page-in-fixed-time/

CodePudding user response:

You did not explain, what your requirements exactly are. Does the data really change every 5 seconds? Or do you not want to miss one update?

To refresh your web-page use this head tag:

<head>
  <title>Product Manager</title>
  <meta charset="utf-8"/>
  <meta http-equiv="refresh" content="5">
</head>

But if the data only changes from time to time, I would use pushing (instead of pulling).

For example with HTML5 server-sent-events: https://www.w3schools.com/html/html5_serversentevents.asp

You can query the db table with a spring boot scheduler every 5 seconds, or you can use messaging on the server side whenever the data changes. (Which I would prefer. This could reduce the CPU load and bandwidth and be more sustainable.)

  • Related