我正在建立一个新的网站,我想拥有多个文件来存储可重复使用的组件(页眉,页脚,,上的常用导入,脚本等)。我还希望每个布局都有文件(结合我定义的组件),例如“主应用程序”,“全屏仪表板”等。 最后,我想创建仅包含所需内容的页面,然后将其插入我选择的布局中。
类似这样的内容:Diagram of files
我看过this question,但这要求我以内容为参数调用java布局,而不是在内容上定义要使用的布局。
我还考虑过仅将“布局”用作模板,然后将+粘贴并粘贴到新的“内容”文件中,然后将占位符编辑掉。
这是一个示例控制器:
@View("ticketing/home")
@Get("/")
public HttpResponse home() {
final Map<String, Object> resultMap = new HashMap<>();
resultMap.put("requests", ticketingClient.getSummaryRequests()); //Returns a list of tickets to be displayed.
return HttpResponse.ok(resultMap); //Maps the resultMap variable to the ticketing/home thymeleaf template
}
这就是我想要的内容文件结构化的方式:
<html>
<head>
<title>Tickets</title>
</head>
<body>
<div>
<p>My Content is here!</p>
<img src="wow.jpeg">
</div>
</body>
</html>
这就是我现在的布局:
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Application Layout</title>
<th:block th:include="./fragments/head.html :: imports"></th:block>
</head>
<body>
<div id="wrapper">
<!-- Sidebar Menu -->
<div th:replace="./fragments/sidebar.html :: sidebar"></div>
<div id="page-wrapper" class="gray-bg">
<!-- Navigation Menu -->
<div th:replace="./fragments/topbar.html :: topbar"></div>
<!-- Page Header -->
<div th:replace="./fragments/pageheader.html :: pageheader"></div>
<!-- Main Content -->
<div class="wrapper wrapper-content animated fadeInUp" th:fragment="content">
<div class="row">
<div class="col-lg-12">
<div class="wrapper wrapper-content">
<p>Content here</p>
</div>
</div>
</div>
</div>
</div>
</div>
<div th:replace="./fragments/footer.html :: footer"></div>
<th:block th:replace="./fragments/scripts.html :: scripts"></th:block>
</body>
</html>
我希望能够使用相同的Java语法(带有批注)来使用我的视图。
相对于布局文件,我宁愿在内容文件中进行更多更改。
答案 0 :(得分:0)
我找到了一个对我有用的答案:
我已经引用了this post,this other post和the post mentioned on the question。
以下是一些代码: 组件:内容
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Content Wrapper</title>
</head>
<body>
<div class="wrapper wrapper-content animated fadeInUp" th:fragment="content(content)">
<!-- Main Content -->
<div class="row">
<div class="col-lg-12">
<div class="wrapper wrapper-content">
<th:block th:replace="${content}"></th:block>
</div>
</div>
</div>
</div>
</body>
</html>
组件:pageheader
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org" th:fragment="pageheader">
<head>
<title>Page Header</title>
</head>
<body>
<div class="row wrapper border-bottom white-bg page-heading" th:fragment="pageheader(title)">
<div class="col-sm-8 col-lg-9">
<!-- Title -->
<h2 th:replace="${title}">This is main title</h2>
<th:block th:if="${breadcrumb}">
<!-- Breadcrumb -->
<ol class="breadcrumb" th:replace="${breadcrumb}">
<li class="breadcrumb-item"><a href="index.html">This is</a></li>
<li class="breadcrumb-item active"><strong>Breadcrumb</strong></li>
</ol>
</th:block>
</div>
<div class="col-sm-4 col-lg-3">
<th:block th:if="${actions}">
<!-- Main Action -->
<div class="title-action btn-group" th:if="${actions}" th:replace="${actions}">
<a href="" class="btn btn-primary">Main Actions</a>
<a href="" class="btn btn-primary">Main Actions</a>
</div>
</th:block>
</div>
</div>
</body>
</html>
布局:应用布局
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org" th:fragment="layout"> <!-- ${content} and ${title} are mandatory -->
<head>
<title th:replace="${title}">Application Layout</title>
<th:block th:include="./fragments/head.html :: imports"></th:block>
</head>
<body>
<div id="wrapper">
<!-- Sidebar Menu -->
<div th:replace="./fragments/sidebar.html :: sidebar"></div>
<div id="page-wrapper" class="gray-bg">
<!-- Navigation Menu -->
<div th:replace="./fragments/topbar.html :: topbar"></div>
<!-- Page Header -->
<div th:replace="./fragments/pageheader.html :: pageheader(title=${title},breadcrumb=${breadcrumb},actions=${actions})"></div>
<!-- Main Content -->
<div class="wrapper wrapper-content animated fadeInUp" th:replace="./fragments/content.html :: content(${content})"></div>
</div>
</div>
<div th:replace="./fragments/footer.html :: footer"></div>
<th:block th:replace="./fragments/scripts.html :: scripts"></th:block>
</body>
</html>
内容:票证
<!DOCTYPE html>
<html lang="en" th:replace="~{layout/applayout.html :: layout(title=~{::title},breadcrumb=~{::ol},content=~{::#main},scripts=~{::#scripts})}" xmlns:th="http://www.thymeleaf.org">
<head>
<title>Homepage</title>
</head>
<body>
<ol class="breadcrumb">
<li class="breadcrumb-item"><a href="index.html">Home</a></li>
<li class="breadcrumb-item active"><strong>wow</strong></li>
</ol>
<div id="main">
<div class="col">
<p>WOW</p>
</div>
<div class="col">
<p>WOW</p>
</div>
<div class="col">
<p>WOW</p>
</div>
</div>
<th:block id="scripts">
</th:block>
</body>
</html>
这样,我可以在内容页面上具有可选参数,这些参数将分别由每个片段处理。