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

问题描述

限时删除!!

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

  • JSPX 最小化了我们不想要的标签,例如 <div class="foo"></div> 变成了 <div class="foo"/> 浏览器的解释不同
  • JSPX 标签必须关闭,而一些 HTML 标签应该保持未关闭,例如 .IE 和 Firefox 无法识别自闭合 标签.
  • 指定 HTML5 文档类型 ()
  • 内联 JavaScript

这个问题是对其他几个都归结为同一问题的回答.我找不到全面的答案,所以我发布了我的发现结果.

相关问题:

解决方案

JSPX 非常适合生成 HTML 和 XHTML.

归结为理解这种语言的 XML 性质.JSPX 是 XML,而 HTML 不是. 含义之一是 JSPX 解析器最小化"空标签,因为 XML 不区分自闭合标签和空标签.这会导致

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

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

${message}

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

<div style="foo">你好</div>

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

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

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

相关问题中的其他建议是在

这会正常工作:

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

产生:

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)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-07 00:37