JSP 文档/JSPX:什么决定了如何在输出中删除制表符/空格/换行符?

发布于 2024-09-03 23:38:37 字数 865 浏览 8 评论 0原文

我有一个格式良好的“JSP 文档”(“XML 中的 JSP”),当生成网页并将其发送给用户时,一些换行符被删除。

现在真正奇怪的部分是:显然“主”.jsp 总是将其换行符删除,但对于主 .jsp 中包含的任何后续 .jsp,换行符似乎被随机删除(有些是存在的,其他人则不然)。

例如,如果我正在查看 Firefox 提供的网页并要求“查看源代码”,我就可以看到生成的内容。

那么,什么决定了何时/如何保留/删除换行符?

这只是我编的一个例子...你能强制一个 .jsp 来服务这个:

<body><div id="page"><div id="header"><div class="title">...

或这个:

<body>
  <div id="page">
    <div id="header">
      <div class="title">...

吗?

我认为删除换行符是为了节省带宽,但如果我想保留它们怎么办?如果我想保持与 .jsp 文件中相同的 XML 缩进怎么办?

这可行吗?

编辑

按照skaffman的建议,我查看了生成的.java文件,“主”文件没有很多out.write,但没有一个写入制表符或换行符。与该文件相反,我从主 .jsp 中包含的所有文件都有很多行,例如:

out.write("\t...\n");

所以我想我的问题仍然完全相同:什么决定了如何在输出中包含/删除制表符/空格/换行符?

I've got a "JSP Document" ("JSP in XML") nicely formatted and when the webpage is generated and sent to the user, some linebreaks are removed.

Now the really weird part: apparently the "main" .jsp always gets all its linebreak removed but for any subsequent .jsp included from the main .jsp, linebreaks seems to be randomly removed (some are there, others aren't).

For example, if I'm looking at the webpage served from Firefox and ask to "view source", I get to see what is generated.

So, what determines when/how linebreaks are kept/removed?

This is just an example I made up... Can you force a .jsp to serve this:

<body><div id="page"><div id="header"><div class="title">...

or this:

<body>
  <div id="page">
    <div id="header">
      <div class="title">...

?

I take it that linebreaks are removed to save on bandwidth, but what if I want to keep them? And what if I want to keep the same XML indentation as in my .jsp file?

Is this doable?

EDIT

Following skaffman's advice, I took a look at the generated .java files and the "main" one doesn't have lots of out.write but not a single one writing tabs nor newlines. Contrary to that file, all the ones that I'm including from that main .jsp have lots of lines like:

out.write("\t...\n");

So I guess my question stays exactly the same: what determines how tabs/space/linebreaks are included/removed in the output?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

月亮是我掰弯的 2024-09-10 23:38:37

根据 JSP 规范

JSP.6.2.3 语义模型

...

为了清楚地解释空白的处理,我们遵循以下结构
XSLT 规范。处理 JSP 文档的第一步是识别
文档的节点。 然后,所有只有空白的文本节点
从文档中删除;唯一的例外是 jsp:text 元素中的节点,
逐字保存。
结果节点的解释如
以下部分。模板数据要么直接传递到响应,要么
通过(标准或自定义)操作进行调解。

因此,如果您想保留空白,则需要将所需部分包装在 中。

As per the JSP specification:

JSP.6.2.3 Semantic Model

...

To clearly explain the processing of whitespace, we follow the structure of the
XSLT specification. The first step in processing a JSP document is to identify the
nodes of the document. Then, all textual nodes that have only white space are
dropped from the document; the only exception are nodes in a jsp:text element,
which are kept verbatim.
The resulting nodes are interpreted as described in the
following sections. Template data is either passed directly to the response or it is
mediated through (standard or custom) actions.

So, if you want to preserve whitespace, you need to wrap the desired parts in <jsp:text>.

瀟灑尐姊 2024-09-10 23:38:37

不是一个非常精确的答案,但根据此消息,Jasper 可能会这样做(我承认我没有检查自己):

我查了一下,没想到
这个问题之前已经被问过。

我一直将 JSP 页面编写为 XML
文档(.jspx 后缀)和一个
差异让我有点恼火
使用简洁的 XML 文档
语法和(2.0 之前)遗留语法
是换行符,还是缺少
前者。

处理后的 XML 文档结果为
标签完整的 XHTML
通过间距或换行符。

我完全理解背后的概念
为什么会出现这种情况?第一个 XML
文档被解析到它的节点
树,然后生成XHTML
基于这棵树。

但是,我理解
白色间距不必
途中迷路。我检查了
贾斯珀代码,特别是
org.apache.jasper.compiler.Node,和
粗略检查发现很多
.trim() 调用,可能是
不必要地删除间距。
不过,我不太熟悉
这段代码果断地说。

总而言之,这可能是一个错误/区域
的改进。无论如何,有没有一个
嵌入换行符的方法
配置?我有信心成为
能够用丑陋、丑陋来实现这些
CDATA 部分,或一些类似的技术
(jsp:text?)...但在我开始之前
麻烦,有没有更简单/更整洁的
方式?

该消息相当古老,但此评论中也报告了相同的行为你的问题似乎暗示它仍然适用。

不确定您是否可以更改此行为(不使用过滤器)。

Not a very precise answer but according to this message, Jasper might be doing this (I admit I didn't check myself):

I had a search around, and don't think
this one has been asked before.

I've been writing my JSP pages as XML
documents (.jspx suffix), and one
difference that annoys me a little
between using the terse XML document
syntax and the (pre-2.0) legacy syntax
are line breaks, or lack of in the
former.

The processed XML document results in
the XHTML where the tags are unbroken
by either spacing or line breaks.

I understand fully the concept behind
why this is the case; the first XML
document is parsed down to it's node
tree, and then the XHTML is generated
based on this tree.

However, I am of the understanding
that the white spacing doesn't have to
be lost along the way. I checked the
jasper code, particularly
org.apache.jasper.compiler.Node, and a
cursory inspection reveals a lot of
.trim() calls, that may be
unnecessarily removing the spacing.
However, I am not so familiar with
this code to say decisively.

In summation, this may be a bug/area
of improvement. Regardless, is there a
way to embed line breaks via
configuration? I feel confident to be
able to achieve these with ugly, ugly
CDATA sections, or some like technique
(jsp:text?)... but before I go to that
trouble, is there an easier/neater
way?

The message is pretty old but the same behavior is also reported in this comment and your question seems to imply it still applies.

Not sure you can change this behavior (without using a Filter).

水溶 2024-09-10 23:38:37
<trim-directive-whitespaces>true</trim-directive-whitespaces>

如果您在 web.xml 中使用上述指令, 内的空格将被去除。给定 BalusC 的回答,该指令仅在您不使用 XML 语法时才有用。

(Tomcat 6.0.16;Servlet 2.5 声明;XML 语法中的 JSP 2.1 声明)

<trim-directive-whitespaces>true</trim-directive-whitespaces>

Whitespace inside <jsp:text> is stripped if you use the above directive in your web.xml. Given the semantic model quoted in BalusC's answer, this directive is only useful if you're not using XML syntax.

(Tomcat 6.0.16; Servlet 2.5 declarations; JSP 2.1 declarations in XML syntax)

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