Java 页眉和页脚的良好 JSP 代码结构
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37499821/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Good JSP code structure for header and footer
提问by Petr Dvo?ák
I currently have following code structure of my JSP pages:
我目前的 JSP 页面有以下代码结构:
MyPage.jsp
我的页面.jsp
<jsp:include page="header.jsp"/>
Specific page content
<jsp:include page="footer.jsp"/>
However, this means that the header and footer code do not have a correct HTML structure. For example, this is simplified header and footer code:
但是,这意味着页眉和页脚代码没有正确的 HTML 结构。例如,这是简化的页眉和页脚代码:
header.jsp
头文件.jsp
<!DOCTYPE html>
<html lang="en">
<head>
<title>${param.pageTitle}</title>
</head>
<body>
<div class="container" style="margin-top: 80px;">
footer.jsp
页脚.jsp
</div>
</body>
</html>
As a result, IDE gives a warning about "missing start tag" / "missing end tag". I don't feel good about disabling the warning entirely since it may find legitimate issues with HTML structure.
因此,IDE 会发出有关“缺少开始标记”/“缺少结束标记”的警告。我不喜欢完全禁用警告,因为它可能会发现 HTML 结构的合法问题。
Is there a cleaner way to structure the JSP code so that I can still reuse header and footer code in some good way?
是否有一种更简洁的方式来构建 JSP 代码,以便我仍然可以以某种好的方式重用页眉和页脚代码?
回答by Gaurav Mahindra
What you are doing is complete wrong way of doing programming. We use include tag to insert a complete page in another page. We do not divide a page in parts. So the correct approach should be to include two separate jsp pages as header and footer in your main jsp page without distorting it. That is your MyPage.jsp should be :-
你正在做的是完全错误的编程方式。我们使用 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>
and header.jsp and footer.jsp should be :-
和 header.jsp 和 footer.jsp 应该是:-
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
Specific page content
</body>
</html>
This is the proper structure of a page.
这是页面的正确结构。
回答by Suraj Shingade
Might be this is helpful. Please have a look.
可能这是有帮助的。请看一看。
\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
Remember
记住
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.
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.
So here is code snippet.
所以这里是代码片段。
1) home.jsp
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
2) 元.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
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
4) 脚注.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>
Thanks :)
谢谢 :)