如何使用 JSPX 生成有效的 HTML? (不是 XHTML)
当尝试使用 JSPX 创建 HTML 页面时,您将面临以下困难:
- JSPX 最小化了我们不希望它出现的标签,例如
变成
,浏览器的解释不同
- JSPX 标签必须关闭,而某些 HTML 标签应保持不关闭,例如
。自闭式
标签不被 IE 和 Firefox 识别。 - 指定 HTML5 doctype (
)
- 内联 JavaScript
这个问题是对其他几个问题的回应,这些问题都归结为同一问题。我找不到全面的答案,因此我发布了我的发现结果。
相关问题:
When trying to create a HTML page with JSPX you will face the following difficulties:
- JSPX minimizes tags we don't want it to, for example
<div class="foo"></div>
becomes<div class="foo"/>
which is interpreted differently by browsers - JSPX tags must be closed, while some HTML tags should remain unclosed, for example
<script...>
. Self-closed<script.../>
tag is not recognized by IE and Firefox. - Specifying HTML5 doctype (
<!DOCTYPE html>
) - Inline JavaScript
This question is a response to a few other that all boil down to the same problem. I couldn't find a comprehensive answer so I'm posting the result of my findings.
Related questions:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
JSPX 非常适合生成 HTML 和 XHTML。
归根结底就是理解这种语言的 XML 本质。 JSPX 是 XML,而 HTML 不是。其中的含义之一是 JSPX 解析器“最小化”空标签,因为 XML 不区分自闭合标签和空标签。这会导致
和标记问题。然而,值得注意的是,虽然 JSPX 文件必须是有效的 XML,但它们生成的输出却不是。因此,使用 JSPX 文件生成 HTML(而不仅仅是 XHTML)是完全正确的。事实上,您可以使用 JSPX 生成任何文本输出,例如 CSV、CSS 或 JS,尽管这会相当不方便。
考虑到上述情况,最干净的解决方案似乎是创建一个带有 htmlScript、htmlDiv 等标签的自定义标签库。这些标签可以像这样使用:
它的 HTML 输出将包含结束标签,无论内容如何:
标签库这样您就可以使用 JSPX 构建 HTML 页面,而无需使用相当丑陋的解决方法。
构建 HTML 页面似乎是 JSPX 最常见的应用之一。没有标准的 HTML 库有点令人惊讶。 JSF 有一个,但如果您使用干净的 JSP,它不会对您有帮助。可能有第三方库填补了这一空白,但我找不到。
如果您不想编写自己的标记库,另一种方法是使用 CDATA:
或:
相关问题中的其他建议是将注释或空
放在 < code>正如 Ralph 指出的,上面的内容不适用于 WebSphere)
或者:
CDATA 在其他一些情况下也能派上用场。其中之一是打印 HTML5 文档类型 (
)。 JSPX 不允许您将 DOCTYPE 声明放入文档中,因为它不是有效的 XML。 JSPX 还定义了 jsp:output 标记,但即使它为空,它也会打印 SYSTEM 属性。解决方法是将 DOCTYPE 包装在页面开头的 CDATA 中:
CDATA 还可用于封装内联 JavaScript。虽然这会因为“<”而产生解析错误符号:
这会很好地工作:
请注意,JSPX 中的 CDATA 按原样输出所有内容,但仍计算 EL 表达式。因此,以下内容:
产生:
希望这有帮助:)
JSPX is perfectly suitable for producing both HTML and XHTML.
It boils down to understanding the XML nature of this language. JSPX is XML while HTML is not. One of the implications is that JSPX parser "minimizes" empty tags because XML does not differentiate between self-closing tags and empty tags. This causes the
<script...>
and<div></div>
tags problems. However, it's worth to note that while JSPX files must be valid XML, the output they produce does not. Thus it's perfectly correct to have a JSPX file producing HTML (not just XHTML). In fact, you could use JSPX to produce any textual output such as CSV, CSS or JS although it would be rather inconvenient.Taking account of the above, the cleanest solution seems to be creating a custom taglib with tags such as htmlScript, htmlDiv, etc. These tags could be used like this:
Its HTML output would contain the closing tag, regardless of the content:
A taglib like this would let you build HTML pages with JSPX without using rather ugly workarounds.
Building HTML pages seems to be one of the most common application of JSPX. It's somewhat surprising that there's no standard HTML library. JSF has one but if you use clean JSP it won't help you. There might be a third-party library filling this gap but I couldn't find one.
If you don't feel like coding your own taglib, an alternative approach is to use CDATA:
or:
Other proposals in the related questions were to put a comment or empty
<jsp:text>
inside<script>
which gives the same result:(as Ralph noted, the above doesn't work with WebSphere)
or:
CDATA also comes in handy in a few other cases. One of them is printing the HTML5 doctype (
<!DOCTYPE html>
). JSPX won't let you put DOCTYPE declaration inside your document because it's not a valid XML. JSPX also deifnes the jsp:output tag but it prints the SYSTEM attribute even when it's empty. A workaround is to wrap the DOCTYPE in CDATA at the beginning of a page:CDATA can also be used to encapsulate inline JavaScript. While this produces parsing error because of the "<" sign:
this will work fine:
Note that CDATA in JSPX outputs everything as-is but still evaluates EL expressions. Thus the following:
produces:
Hope this helps :)
推荐的方式是:
//
//]]>;
The recommended way is :
<script type="text/javascript">
//<![CDATA[
//]]>
</script>