【问题标题】:How to generalize html pages common parts in MVC Spring application如何在MVC Spring应用中泛化html页面常用部分
【发布时间】:2015-04-21 11:35:21
【问题描述】:

我有一个 Spring MVC Web 应用程序,它只使用 html 页面,我的第一个问题是概括所有 html 页面中的页眉、页脚和侧菜单,以便它们来自所有页面中的一个来源,我使用jquery.load 方法来模拟 jsp 页面的 include 指令,但是这样,我必须在所有页面中添加加载代码,我希望你帮我实现另一种方法,我总是返回一个模板页面,其中包含页眉、页脚和侧边菜单,但是根据最初用户请求的页面动态加载内容,你能告诉我如何在只允许 html 页面的 Spring MVC 应用程序中实现这一点。

【问题讨论】:

  • 谷歌例如 apache 瓦片、thymeleaf。
  • Sitemesh 是要走的路

标签: html spring-mvc


【解决方案1】:

将 Spring MVC 与 Thymeleaf 结合使用将是您的最佳选择。这个article 将是一个很好的开始

它为您提供了一种设计与给定示例类似的布局文件的方法,同时仍处于纯 HTML 文件的领域

<html xmlns:th="http://www.thymeleaf.org">
 <head th:include="thymeleaf/layout :: headerFragment">
 <!-- replaced with fragment content -->
 <!—- 'thymeleaf/layout' refers to /thymeleaf/layout.html on the filesystem -->
 </head>

 <body>

 <div th:include="thymeleaf/layout :: menuFragment">
 </div>
 <div th:if="${not #lists.isEmpty(users)}">
 <table>
   …
   <tbody>
     <tr th:each="user : ${users}">
      <td th:text="${user.firstName}">John</td>
      <td th:text="${user.lastName}">Smith</td>
     </tr>
   </tbody>
  </table>
 </div>
 </body>
</html>

来自博客的引述

Thymeleaf 将自己定义为 XML / XHTML / HTML5 模板引擎。

它不是基于 JSP,而是基于一些带有 一点命名空间的魔法。

【讨论】:

    猜你喜欢
    • 2018-12-25
    • 1970-01-01
    • 2013-10-10
    • 1970-01-01
    • 1970-01-01
    • 2016-05-30
    • 2013-07-16
    • 2012-10-24
    • 2012-12-28
    相关资源
    最近更新 更多