在百里香中使用th:if和th:replcae动态更改页面

时间:2019-01-18 12:41:49

标签: java spring-boot thymeleaf

这是我的控制器:

@RequestMapping(value="/user/edit/{id}", method = RequestMethod.GET)
public String editForm(@PathVariable Integer id,Model model) {
    model.addAttribute("UserInfo", userService.getUserById(id));
    return "EditUser";
}


   @RequestMapping(value="/moduleList", method = RequestMethod.GET)
    public String moduleList(Model model) {
        model.addAttribute("moduleCommand", new Module());
        model.addAttribute("moduleList", applicationService.getAllModules());
        return "modulelist";
    }

rightSection.html

    <div th:fragment="rightSectionBar">
    <div class="container body">
        <div class="title_section" style="margin-top: 50px;">
            <div class="col-md-2 heading">
                <h4>Home</h4>
            </div>
        </div>
        <div class="col-md-9 right_col">
            <div class="clearfix"></div>
            <!-- page content -->
            <span th:replace="fragments/contents::dashboard"></span> 

//I want to see below content on calling "/user/edit/{id}" url
            <span th:replace="fragments/EditUser::editUsersBar"></span>

//I want to see below content on calling "/moduleList" url
            <span th:replace="fragments/modulelist::modulesListBar"></span>
        </div>
    </div>
</div>

我试图在每个页面上使侧边菜单和顶部导航保持相同,并且只想更改右侧的内容。

1 个答案:

答案 0 :(得分:1)

您可以为此目的使用th:blockth:if。 Thymeleaf将执行th:block中的属性,因为th:block只是一个属性容器,因此它将消失。

<div class="col-md-9 right_col">
<div class="clearfix"></div>
<span th:replace="fragments/contents::dashboard"></span> 

<th:block th:if="${UserInfo}">
    <span th:replace="fragments/EditUser::editUsersBar"></span>
</th:block>
<th:block th:if="${moduleCommand}">
    <span th:replace="fragments/modulelist::modulesListBar"></span>
</th:block>
</div>