如何使用 JSPX 生成有效的 HTML? (不是 XHTML)

发布于 2024-10-11 13:13:07 字数 893 浏览 2 评论 0原文

当尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏末 2024-10-18 13:13:07

JSPX 非常适合生成 HTML 和 XHTML。

归根结底就是理解这种语言的 XML 本质。 JSPX 是 XML,而 HTML 不是。其中的含义之一是 JSPX 解析器“最小化”空标签,因为 XML 不区分自闭合标签和空标签。这会导致

标记问题。然而,值得注意的是,虽然 JSPX 文件必须是有效的 XML,但它们生成的输出却不是。因此,使用 JSPX 文件生成 HTML(而不仅仅是 XHTML)是完全正确的。事实上,您可以使用 JSPX 生成任何文本输出,例如 CSV、CSS 或 JS,尽管这会相当不方便。

考虑到上述情况,最干净的解决方案似乎是创建一个带有 htmlScript、htmlDiv 等标签的自定义标签库。这些标签可以像这样使用:

<html:div styleClass="foo" selfClosing="false">${message}<html:div>

它的 HTML 输出将包含结束标签,无论内容如何:

<div style="foo"></div>
<div style="foo">Hello</div>

标签库这样您就可以使用 JSPX 构建 HTML 页面,而无需使用相当丑陋的解决方法。

构建 HTML 页面似乎是 JSPX 最常见的应用之一。没有标准的 HTML 库有点令人惊讶。 JSF 有一个,但如果您使用干净的 JSP,它不会对您有帮助。可能有第三方库填补了这一空白,但我找不到。

如果您不想编写自己的标记库,另一种方法是使用 CDATA:

<![CDATA[<script type="text/javascript" src="/js/jquery-1.4.4.min.js">]]>

或:

<![CDATA[<script type="text/javascript" src="/js/jquery-1.4.4.min.js"></script>]]>

相关问题中的其他建议是将注释或空 放在 < code>

<script type="text/javascript" src="/js/jquery-1.4.4.min.js"><!-- Prevent self-closing --></script>

正如 Ralph 指出的,上面的内容不适用于 WebSphere)
或者:

<script type="text/javascript" src="/js/jquery-1.4.4.min.js"><jsp:text></jsp:text></script>

CDATA 在其他一些情况下也能派上用场。其中之一是打印 HTML5 文档类型 ()。 JSPX 不允许您将 DOCTYPE 声明放入文档中,因为它不是有效的 XML。 JSPX 还定义了 jsp:output 标记,但即使它为空,它也会打印 SYSTEM 属性。解决方法是将 DOCTYPE 包装在页面开头的 CDATA 中:

<![CDATA[<!DOCTYPE html>]]>

CDATA 还可用于封装内联 JavaScript。虽然这会因为“<”而产生解析错误符号:

<script type="text/javascript">
    var x = 7 < 5;
</script>

这会很好地工作:

<script type="text/javascript">
    <![CDATA[
    var x = 7 < 5;
    ]]>
</script>

请注意,JSPX 中的 CDATA 按原样输出所有内容,但仍计算 EL 表达式。因此,以下内容:

<![CDATA[
<jsp:expression>"asd " + "def"</jsp:expression>
${1 + 2}
]]>

产生:

<jsp:expression>"asd " + "def"</jsp:expression>
3

希望这有帮助:)

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:

<html:div styleClass="foo" selfClosing="false">${message}<html:div>

Its HTML output would contain the closing tag, regardless of the content:

<div style="foo"></div>
<div style="foo">Hello</div>

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:

<![CDATA[<script type="text/javascript" src="/js/jquery-1.4.4.min.js">]]>

or:

<![CDATA[<script type="text/javascript" src="/js/jquery-1.4.4.min.js"></script>]]>

Other proposals in the related questions were to put a comment or empty <jsp:text> inside <script> which gives the same result:

<script type="text/javascript" src="/js/jquery-1.4.4.min.js"><!-- Prevent self-closing --></script>

(as Ralph noted, the above doesn't work with WebSphere)
or:

<script type="text/javascript" src="/js/jquery-1.4.4.min.js"><jsp:text></jsp:text></script>

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[<!DOCTYPE html>]]>

CDATA can also be used to encapsulate inline JavaScript. While this produces parsing error because of the "<" sign:

<script type="text/javascript">
    var x = 7 < 5;
</script>

this will work fine:

<script type="text/javascript">
    <![CDATA[
    var x = 7 < 5;
    ]]>
</script>

Note that CDATA in JSPX outputs everything as-is but still evaluates EL expressions. Thus the following:

<![CDATA[
<jsp:expression>"asd " + "def"</jsp:expression>
${1 + 2}
]]>

produces:

<jsp:expression>"asd " + "def"</jsp:expression>
3

Hope this helps :)

最终幸福 2024-10-18 13:13:07

推荐的方式是:

//

    //your javascript goes here
    function myFuncy(){
        if( a > b || a >= b && b && c){
            X;
        }
    }

//]]>;

The recommended way is :

<script type="text/javascript">

//<![CDATA[

    //your javascript goes here
    function myFuncy(){
        if( a > b || a >= b && b && c){
            X;
        }
    }

//]]>

</script>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文