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

[英]How to produce valid HTML with JSPX? (not XHTML)


When trying to create a HTML page with JSPX you will face the following difficulties:

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

  • 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最小化了我们不希望它的标记,例如
    变成
    ,浏览器会对它们进行不同的解释
  • 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.
  • 必须关闭JSPX标记,而某些HTML标记应保持未关闭,例如
  • Specifying HTML5 doctype (<!DOCTYPE html>)
  • 指定HTML5 doctype(<!DOCTYPE html>)
  • Inline JavaScript
  • 内联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:

相关问题:

2 个解决方案

#1


24  

JSPX is perfectly suitable for producing both HTML and XHTML.

JSPX非常适合生成HTML和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.

归结为理解这种语言的XML特性。 JSPX是XML而HTML则不是。其中一个含义是JSPX解析器“最小化”空标记,因为XML不区分自闭标记和空标记。这会导致

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:

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

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

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

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

<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.

像这样的taglib可以让你使用JSPX构建HTML页面而不使用相当丑陋的变通方法。

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.

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

If you don't feel like coding your own taglib, an alternative approach is to use CDATA:

如果您不想编写自己的taglib,另一种方法是使用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:

(正如Ralph所指出的,上述内容不适用于WebSphere)或:

<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在其他一些案例中也派上用场。其中一个是打印HTML5 doctype(<!DOCTYPE html>)。 JSPX不允许您将DOCTYPE声明放在文档中,因为它不是有效的XML。 JSPX还可以对jsp:output标记进行deifnes,但即使它是空的,它也会打印SYSTEM属性。解决方法是将DOCTYPE包装在页面开头的CDATA中:

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

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

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

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

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

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

produces:

生产:

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

Hope this helps :)

希望这可以帮助 :)

#2


0  

The recommended way is :

推荐的方法是:

<script type="text/javascript">

//<![CDATA[

// <![CDATA [

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

//]]>

//]]>

</script>

智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2011/01/09/6b1dc101ff653c5f31de716b28d8deb8.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告