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

时间:2014-12-13 16:52:42

标签: spring thymeleaf

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

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

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

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

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

return "cutleryCustomerSearch :: customerSearch/customersTable";

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

这是我的ajax请求:

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

2 个答案:

答案 0 :(得分:5)

我在这篇博文中找到了一个例子:Thymeleaf integration with Spring (Part 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";
}

在页面&#34;结果&#34;将有一个名为&#34; resultsList&#34;的块(片段),如下例所示:

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

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

答案 1 :(得分:2)

嗯,这很容易......

return "fragments/customerSearch :: customersTable";