簡體   English   中英

插入后如何自動更新視圖

[英]How to update automatically view after insert

在插入數據庫后,我試圖自動更新視圖(此處為emprunts列表)。 我使用Springboot,H2,JPA,Thymeleaf,...

有辦法嗎? 還是在插入后使用Get請求刷新頁面?

非常感謝!

HTML /視圖

...
<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>
...

調節器

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

如果您的視圖技術是百里香,則返回html頁面而不是返回ResponseEntity(因為響應實體將以json格式返回數據)並在model屬性中添加數據。

有辦法嗎? 還是在插入后使用Get請求刷新頁面?

無需刷新頁面,只需從控制器返回html頁面,如下所示。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM