Java如何使用 包含其他页面?
该<jsp:include/>标记用于将JSP页面的另一个页面片段包含到另一个页面中。当您拥有适用于所有页面的通用页面(例如页眉,页脚或菜单)时,此功能很有用。
<%@ page contentType="text/html;charset=UTF-8" %>
<!DOCTYPE html>
<html>
<head>
<title><jsp:include/> Demo</title>
</head>
<body>
<div id="header">
<jsp:include page="common/header.jsp"/>
</div>
<div id="main">
Lorem ipsum dolor sit amet, consectetur adipisicing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna
aliqua.
</div>
<div id="footer">
<jsp:include page="common/footer.jsp"/>
</div>
</body>
</html>下面是该页面片段header.jsp,footer.jsp和menu.jsp。它们全部都放置在与文件相同位置的公用文件夹中index.jsp。
header.jsp
<strong><jsp:include/> Demo</strong> <hr/> <jsp:include page="menu.jsp"/>
footer.jsp
<hr/> © 2019 Kode Java Org.
menu.jsp
<a href="/index.jsp">HOME</a>
当您从servlet容器(例如ApacheTomcat)访问页面(http://localhost:8080/include/main.jsp)时,您将完整显示页面,其中包含页眉,菜单,内容和页脚。
这是我们示例的目录结构:
.
├── pom.xml
└── src
└── main
└── webapp
├── WEB-INF
│ └── web.xml
├── include
│ ├── common
│ │ ├── footer.jsp
│ │ ├── header.jsp
│ │ └── menu.jsp
│ └── main.jsp
└── index.jsp该web.xml配置文件。
<?xml version="1.0" encoding="UTF-8"?>
<web-app
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!-- Config here. -->
</web-app>