Royce
Royce

Reputation: 1595

How to update automatically view after insert

I'm trying to automatically update view (here list named emprunts) after insert into my database. I use Springboot, H2, JPA, Thymeleaf, ...

There is a way to do it? Or have I to refresh the page with Get request after the insert ?

Thanks a lot!

HTML / View

...
<div id="collapseThree" class="collapse" aria-labelledby="headingThree" data-parent="#accordionEmprunts">
   <div class="card-body">
      <table class="table">
            <thead>
                  <tr>
                       <th scope="col">Usager</th>
                       <th scope="col">Exemplaire</th>
                       <th scope="col">Date de rendu</th>
                       <th scope="col">Statut</th>
                   </tr>
             </thead>
             <tbody>
                   <th:block th:each="emprunt : ${emprunts}">
                       <tr>
                       <!--/*@thymesVar id="emprunt" type="bibliotheque.model.Emprunt"*/-->
                           <td th:text="${emprunt.getUsager().getMail()}"></td>
                           <td th:text="${emprunt.getExemplaire().getOeuvre().getTitre()}"></td>
                           <td th:text="${emprunt.getDaterendu()}"></td>
                           <td th:text="${emprunt.getStatut()}"></td>
                        </tr>
                   </th:block>
              </tbody>
         </table>
    </div>
</div>
...

Controller

@PostMapping
public ResponseEntity<?> newEmprunt(HttpEntity<String> infoEmprunt) throws IOException {
    ...
    repository.save(object);
    return new ResponseEntity<>(HttpStatus.CREATED);
}

Upvotes: 0

Views: 224

Answers (1)

Alien
Alien

Reputation: 15878

If your view technology is thymeleaf then return html page instead of returning ResponseEntity(because response entity will return data in json format) and add data in model attribute.

There is a way to do it? Or have I to refresh the page with Get request after the insert ?

No need to refresh page just return html page from controller like below.

Emprunt emprunt = repository.save(object);    
model.addAttribute("emprunt", emprunt);    
return "show"; //show.html page

Upvotes: 1

Related Questions