如何从控制器访问片段中的片段?

Pat*_*mle 2 spring thymeleaf

我有一个名为"cutleryCustomerSearch"的视图,其中包含(替换)一个片段:

<div id="content">
     <div th:replace="fragments/customerSearch :: customerSearch"></div>
</div>
Run Code Online (Sandbox Code Playgroud)

在这个片段我有一个表,我想通过ajax更新:

<table id="customersTable" th:fragment="customersTable">
Run Code Online (Sandbox Code Playgroud)

我如何设置处理ajax请求的控制器方法的返回?目前我有(不工作):

return "cutleryCustomerSearch :: customerSearch/customersTable";
Run Code Online (Sandbox Code Playgroud)

但是它没有设置内容,只是删除了表.它只是工作正常,直到我破坏"customerSearch".

这是我的ajax请求:

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

Gui*_*sta 5

我在这篇博文中找到了一个例子:Thymeleaf与Spring集成(第2部分).

Spring MVC的Controller将返回字符串"results :: resultsList",如下所示:

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

在"结果"页面中,将有一个名为"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>
Run Code Online (Sandbox Code Playgroud)

它应该有帮助......