JSP HTML可以包含两个HEAD标记吗

qybjjes1  于 2022-12-07  发布在  其他
关注(0)|答案(6)|浏览(174)

在我的Web应用程序中,我得到了Header.jsp文件,它包含默认的头内容。我在每个页面的body标记中使用jsp:include标记将它包含在所有其他页面中。
jsp包含自己的HEAD标记来指定默认的 meta标记、链接样式表、脚本和一些HTML元素。同时,我将在所有其他单独的页面中使用另一组HEAD标记来定义标题、页面特定的脚本和样式表。
例如:

标题.jsp

<head>
   <link rel="shortcut icon" href="<%=request.getContextPath()%>/images/favicon.ico" type="image/x-icon" />
   <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
   <script src="js/jquery.js"></script>
   <link rel=stylesheet type="text/css" href="dashboard.css" >
</head>
<h2>Dashboard</h2>

主要.jsp

<!DOCTYPE html> 
<html>
   <head>
      <title>Main page</title>
      <script src="main.js"></script>
   </head>
   <body>
      <jsp:include page="Header.jsp" flush="true" />
      .....
      other HTML contents specific to main page
      .....
   </body>
</html>

这样做是否有效?

r6vfmomb

r6vfmomb1#

The short answer is YES.

It's not a good solution but it will absolutely work.
People usually answer these questions in theory, like "no, because it's not valid by the standards". That's right, it's not. Future browsers might not support it, some source parsers may get confused, HR/IT experts checking out your portfolio may think you know less than Jon Snow, and all sorts of bad things. In theory. But it does happen out there in the real world, and browsers are not stupid: they know what you mean, they will take both head tags and work as expected.
And it's not just by chance.
They have very good reasons:

1. Head tag is optional.(see notes below the article!)

Browsers accept headtag-like contents even outside it, so in effect they ignore the tag itself completely. And if they ignore one, they will probably ignore several, too.

2. Visitors are precious.

Browsers want you to enjoy your time. They want to show you the best possible page they can compose out of the mess they've got. It's their intention, they want to show you that the internet works, not teach you how bad your favorite website is. If they can find out what the html code wants to express (and there's no deadly ambiguity in the structure), they will do their best to fix the page.

3. Bad markup tolerance is a thing.

Browsers are not just patient and forgiving, but sometimes they do acrobatic moves to get your stuff working. Look at this horrible mess:

<!-- no doctype! -->
<!-- no HTML tag! we're all gonna die! -->
<head>
    <style>
        body {background:#002233;}
    </style>
</head>
<head><!-- let's twist again! -->
    <style>
        body {color:white}
    </style>
<!-- we didn't even close the second one!! -->

See this text?<br>
With the background AND color properly set?<br>
<br>
Your browser's quite a badass.

About browser tolerance, here's a lot more with super-ugly examples - make sure you forget everything you've seen when you're back!)
So yes, of course, the principle is "be a good friend of your browser", no matter how cleverly it fixes your mistakes. But if you wake up in a dark dungeon with hungry lions around and your only way out is using two tags - well, don't hesitate! It's not broken syntax, it's not a serious violation of HTML5 rules - it's no more than a convenient cheat. And don't fall for the widespread myth that non-standard, non-tidy sites prosper a lot worse: people usually just don't know for sure and want to stay on the safe side. Typically they are the ones who describe hell as a place where validator-failing web authors go.

TLDR: In practice, two head tags work.

Now please have only one if possible.

ADDITIONAL NOTES

As @StanislavBerkov pointed out, both W3C and MDN suggests that the HEAD tag is implied, meaning it's probably better to just leave the head tag entirely. I would not recommend this approach if you have the standard option of using only one of it, but having none is apparently better than having two. The documentation is not very clear around the topic so make sure you test everything in the major browsers - but again, in practice, you'll face no issues.

esyap4oy

esyap4oy2#

根据标准,它是无效的
相关部分:
4.2.1头部元件
类别:无。
可使用此元素的上下文:作为html元素中的第一个元素。
第二个<head>元素不会是html文档中的第一个元素。

pftdvrlh

pftdvrlh3#

回答得好@Gwenc37。您可以在任何其他标签中使用任何标签,但最好始终遵守W3C标准和规范。在项目的后期,您可能会遇到HTML在浏览器中无法正确解析的情况,甚至出现更糟糕的中断。
为了安全起见,最好遵守W3C标准。这样你就不会出错。希望这对你有帮助。

wgxvkvu9

wgxvkvu94#

这里有一个你可以尝试的想法
在你的大脑里做这个

<!DOCTYPE html> 
<html>
<head>
   <link rel="shortcut icon" href="<%=request.getContextPath()%>/images/favicon.ico" type="image/x-icon" />
   <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
   <script src="js/jquery.js"></script>
   <link rel=stylesheet type="text/css" href="dashboard.css" >

注意,我省略了end head标签。然后在所有文件中,你要么关闭head标签,要么添加额外的header内容,然后关闭head标签,例如

<title>Main page</title>
      <script src="main.js"></script>
   </head>
   <body>
      <jsp:include page="Header.jsp" flush="true" />
      .....
      other HTML contents specific to main page
      .....
   </body>
</html>

至于你的页面标题,你可以运行一点php来确定你在哪个页面上。

hfyxw5xn

hfyxw5xn5#

根据W3C标准,不!你不能拥有它。
在您的例子中,您使用JSP作为服务器端脚本。这个问题可以通过对样式表/脚本/其他html元素使用CONSTANTS来解决。
您只需要按照页面要求在“main.jsp”文件中添加条件。

lfapxunr

lfapxunr6#

根据W3C标准,您不能有两个HEAD标记。
关于您的问题,您可以调用没有HEAD标记的header.jsp文件,也可以重命名为scripts.jsp或constants.jsp
例如:
Header.jsp

<link rel="shortcut icon" href="<%=request.getContextPath()%>/images/favicon.ico" type="image/x-icon" />
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
<script src="js/jquery.js"></script>
<link rel=stylesheet type="text/css" href="dashboard.css" >

Main.jsp

<!DOCTYPE html>
<html>
<head> 
<title>Main page</title>
<script src="main.js"></script>
<jsp:include page="Header.jsp" flush="true" />
</head>
<body> 
..... 
other HTML contents specific to main page
..... 
</body>
</html>

相关问题