2014-12-13 107 views
2

我有一個名爲「cutleryCustomerSearch」,其中包括視圖(替換)片段:在這個片段,我有我喜歡通過AJAX更新表如何從控制器訪問片段中的片段?

<div id="content"> 
    <div th:replace="fragments/customerSearch :: customerSearch"></div> 
</div> 

<table id="customersTable" th:fragment="customersTable"> 

我如何必須設置處理ajax請求的我的控制器方法的返回值?目前我有(不工作):

return "cutleryCustomerSearch :: customerSearch/customersTable"; 

但它沒有設置內容,它只是刪除表。 它只是工作正常,直到我fragmentize「customerSearch」。

這裏我的Ajax請求:

$.ajax({ 
     type : 'POST', 
     cache : false, 
     url  : form.attr('action'), 
     data : form.serialize(), 
     success : function(data) { 
      $("#customersTable").html(data); 
     } 
    }); 

回答

2

那麼它是那麼容易......

return "fragments/customerSearch :: customersTable"; 
4

我發現這個博客帖子的例子:Thymeleaf integration with Spring (Part 2)。在這個例子中

@RequestMapping(value = "/guests", method = RequestMethod.GET) 
public String showGuestList(Model model) { 
    model.addAttribute("guests", hotelService.getGuestsList()); 
    return "results :: resultsList"; 
} 

在網頁 「結果」 會有一個塊(片段)命名爲 「resultsList」,如::

一個Spring MVC的Controller將返回字符串"results :: resultsList",這樣

<div th:fragment="resultsList" th:unless="${#lists.isEmpty(guests)}" class="results-block"> 
    <table> 
     <thead> 
      <tr> 
       <th th:text="#{results.guest.id}">Id</th> 
       <th th:text="#{results.guest.surname}">Surname</th> 
       <th th:text="#{results.guest.name}">Name</th> 
       <th th:text="#{results.guest.country}">Country</th> 
      </tr> 
     </thead> 
     <tbody> 
      <tr th:each="guest : ${guests}"> 
       <td th:text="${guest.id}">id</td> 
       <td th:text="${guest.surname}">surname</td> 
       <td th:text="${guest.name}">name</td> 
       <td th:text="${guest.country}">country</td> 
      </tr> 
     </tbody> 
    </table> 
</div> 

它應該有幫助...