2014-12-13 115 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> 

它应该有帮助...