假设我有一个有 10 个页面的网站。每个页面都由 servlet 调用。例如,我有一个 servlet“index”,它将 forward()
发送到“index.jsp”。
在我的 index.jsp 中,我有 2 个包含页眉和页脚的内容
...
<jsp:include page="header.jsp" >
home page text
<jsp:include page="footer.jsp" >
...
现在我有 10 个与索引页类似的页面,我的意思是我有 10 个包含页眉和页脚的页面。
假设我决定删除页脚:我应该编辑 10 页。
我想知道是否有什么东西可以让我只使用一个页面,并动态地仅显示页面的“内容”(主页、联系人、ecc),请记住,我使用 servlet 来获取每个页面内容(使用forward())。
最佳答案
您需要的是Apache Tiles Framework ,
Tiles allows authors to define page fragments which can be assembled into a complete pages at runtime. These fragments, or tiles, can be used as simple includes in order to reduce the duplication of common page elements or embedded within other tiles to develop a series of reusable templates. These templates streamline the development of a consistent look and feel across an entire application.
它包含配置文件,因此您可以单独在一个文件中进行编辑,这样所做的更改将共同反射(reflect)在所有 jsp 文件中
不错的Startup tutorial在这里
关于java - 如何使用jsp和servlet创建模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25161586/