如何使用JSPX生成有效的HTML? (而不是XHTML) [英] How to produce valid HTML with JSPX? (not XHTML)

查看:122
本文介绍了如何使用JSPX生成有效的HTML? (而不是XHTML)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当试图用JSPX创建HTML页面时,您将面临以下困难:


  • JSPX最大程度地减少了我们不需要的标签例如< div class =foo>< / div> 变为< div class =foo/> 这是由浏览器以不同的方式解释

  • 必须关闭JSPX标记,而一些HTML标记应保持未关闭状态,例如< script。 ..> 。自封< script ... /> 标签无法被IE和Firefox识别。 code><!DOCTYPE html> )

  • 嵌入式JavaScript



这个问题是对其他几个问题的回应,都归结为同样的问题。

相关问题:


ul>

  • GlassFish v3上的jspx脚本元素

  • 保留JSPX (< div>< / div>!=< div />)


  • 解决方案

    JSPX非常适合同时生成HTML和XHTML。



    它归结为理解这种语言的XML特性。 JSPX是XML,而HTML不是。其中一个含义是JSPX解析器最小化了空标签,因为XML不区分自闭标签和空标签。这会导致< script ...> < div>< / div> 标记问题。但是,值得注意的是,尽管JSPX文件必须是有效的XML,它们生成的输出不是。因此,生成HTML的JSPX文件(不仅仅是XHTML)是完全正确的。实际上,您可以使用JSPX生成任何文本输出,例如CSV,CSS或JS,尽管这样会相当不方便。

    考虑到上述情况,最干净的解决方案似乎正在创建一个自定义taglib,其中包含htmlScript,htmlDiv等标签。这些标签可以像这样使用:

     < ; html:div styleClass =fooselfClosing =false> $ {message}< html:div> 

    无论内容如何,​​其HTML输出都会包含结束标记:

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

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



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



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

     <![CDATA [< script type =text / javascriptsrc =/ js /的jquery-1.4.4.min.js>]]> 

    或:

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

    相关问题的其他建议是发表评论或为空< jsp:text> < script> 里面给出了相同的结果:

     < script type =text / javascriptsrc =/ js / jquery-1.4.4.min.js><! - 防止自动关闭 - > < /脚本> 

    (正如Ralph指出的,以上不适用于WebSphere)
    或:

     < script type =text / javascriptsrc =/ js / jquery-1.4.4.min.js> ;< JSP:文本>< / JSP:文本>< /脚本> 

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

     <![CDATA [<!DOCTYPE HTML>]]> 

    CDATA也可用于封装内联JavaScript。虽然这会由于<而产生解析错误sign:

     < 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:表达>
    $ {1 + 2}
    ]]>

    产生:

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

    希望这有助于:)


    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:

    解决方案

    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 :)

    这篇关于如何使用JSPX生成有效的HTML? (而不是XHTML)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆