我目前有以下JSP页面的代码结构:
MyPage.jsp
<jsp:include page="header.jsp"/>
Specific page content
<jsp:include page="footer.jsp"/>
但是,这意味着页眉和页脚代码没有正确的HTML结构。例如,这是简化的页眉和页脚代码:
header.jsp中
<!DOCTYPE html>
<html lang="en">
<head>
<title>${param.pageTitle}</title>
</head>
<body>
<div class="container" style="margin-top: 80px;">
footer.jsp中
</div>
</body>
</html>
因此,IDE会发出关于&#34;缺少开始标记的警告&#34; /&#34;缺少结束标记&#34;。我不会完全禁用警告,因为它可能会发现HTML结构的合法问题。
是否有更简洁的方法来构建JSP代码,以便我仍然可以以一种好的方式重用页眉和页脚代码?
答案 0 :(得分:8)
这可能会有所帮助。请看看。
\home.jsp // Base Page
\parts\meta.jsp // To hold page meta information. Useful when working with SEO
\parts\header.jsp // Resources CSS, images,
\parts\footer.jsp // Resources JS
记住
Use <%@include> because it is static include and <jsp:include> is dynamic include.
When you use <jsp:include> the file will be included at runtime.
When you use <%@include> file will be included at compile time.
所以这里是代码片段。
1)home.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<%@ include file="parts/meta.jsp" %>
<title>Home Page</title>
<%@ include file="parts/header.jsp" %>
</head>
<body>
<div class="view">
<div class="pages">
<jsp:include page="parts/page-body.jsp"></jsp:include>
</div>
</div>
<%@ include file="parts/footer.jsp" %>
</body>
</html>
2)meta.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<meta charset="utf-8">
<meta name="viewport"
content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no, minimal-ui">
<meta name="mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
3)header.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<link href="http://fonts.googleapis.com/css?family=Roboto:400,300,500,700" rel="stylesheet" type="text/css">
<link rel="stylesheet" href="css/my-page.css">
<link rel="stylesheet" href="css/my-icon.css">
<link rel="icon" href="img/icon.png">
4)footer.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<script type="text/javascript" src="js/my-app.js"></script>
<script type="text/javascript" src="js/my-app-service.js"></script>
谢谢:)
答案 1 :(得分:-7)
您正在做的是完全错误的编程方式。我们使用include标签在另一个页面中插入一个完整的页面。我们不会将页面分成几部分。因此,正确的方法应该是在主jsp页面中包含两个单独的jsp页面作为页眉和页脚而不会扭曲它。那是你的MyPage.jsp应该是: -
<!DOCTYPE html>
<html lang="en">
<head>
<title>${param.pageTitle}</title>
</head>
<body>
<jsp:include page="header.jsp"/>
Specific page content
<jsp:include page="footer.jsp"/>
</body>
</html>
并且header.jsp和footer.jsp应该是: -
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
Specific page content
</body>
</html>
这是页面的正确结构。