页眉和页脚的JSP代码结构很好

Pet*_*řák 8 html java jsp

我目前有以下JSP页面的代码结构:

MyPage.jsp

<jsp:include page="header.jsp"/>
Specific page content
<jsp:include page="footer.jsp"/>
Run Code Online (Sandbox Code Playgroud)

但是,这意味着页眉和页脚代码没有正确的HTML结构.例如,这是简化的页眉和页脚代码:

header.jsp中

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>${param.pageTitle}</title>
    </head>
    <body>
        <div class="container" style="margin-top: 80px;">
Run Code Online (Sandbox Code Playgroud)

footer.jsp中

        </div>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

因此,IDE会发出有关"缺少开始标记"/"缺少结束标记"的警告.我完全禁用警告感觉不太好,因为它可能会发现HTML结构的合法问题.

有没有更简洁的方法来构建JSP代码,以便我仍然可以以一些好的方式重用页眉和页脚代码?

Sur*_*ade 9

可能这是有帮助的.请看一看.

\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
Run Code Online (Sandbox Code Playgroud)

记得

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>
Run Code Online (Sandbox Code Playgroud)

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">
Run Code Online (Sandbox Code Playgroud)

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">
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

谢谢 :)


Gau*_*dra -9

你所做的完全是错误的编程方式。我们使用 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>
Run Code Online (Sandbox Code Playgroud)

header.jsp 和 footer.jsp 应该是:-

<!DOCTYPE html>
<html lang="en">
    <head>
    </head>
    <body>
          Specific page content
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

这是页面的正确结构。

  • 这不会导致他想要避免的事情发生吗?它将向浏览器发送一个包含多个嵌套 html 标签和多个嵌套 body 标签的页面。怎样才算正确呢? (3认同)
  • 您错了。它确实发生了。我在 Chrome、Firefox 和 IE 中测试了您的代码。正如我所描述的,它们都显示了源代码。但是,它们足够聪明,可以正确显示页面。如果您使用 IE,请查看调试器以查看实际的源代码。 (3认同)