java - 如何使用jsp和servlet创建模板

标签 java jsp servlets jstl

假设我有一个有 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/

相关文章:

java - 多个 Volley 请求的问题

java - 如何将数据存储到数组以及从数组中求和数据

jsp - Tomcat JSP 身份验证失败

java - JSP——在表单过程中提供 HTTP 500

c# - 针对多种编程语言/平台

java - 如何强制接口(interface)出现在所有继承级别上

jsp - 如何注入(inject)Struts <s :property/> tag into another tag的值

javascript - 向服务器查询文件

java - 如何在JSP/Servlet中设置最大执行时间?

java - 为什么我们无法从jsp声明中访问request?