JSP Document / JSPX:什么决定了输出中如何删除制表符/空格/换行符? [英] JSP Document/JSPX: what determines how tabs/space/linebreaks are removed in the output?

查看:330
本文介绍了JSP Document / JSPX:什么决定了输出中如何删除制表符/空格/换行符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个很好地格式化的JSP文档(JSP in XML),当生成网页并发送给用户时,会删除一些换行符。

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.

现在非常奇怪的部分:显然主.jsp总是将所有删除它的换行符,但对于主.jsp中包含的任何后续.jsp,换行符似乎是随机删除的(某些在那里,其他人不是)。

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

例如,如果我正在查看从Firefox提供的网页并要求查看源代码,我会看到生成的内容。

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?

这只是我编写的一个例子......你可以强制.jsp来提供这个:

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

或者:

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

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

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?

这可行吗?

编辑

按照skaffman的建议,我看了一下生成的.java文件,而主文件没有很多 out.write ,但没有一个写标签或新行。与该文件相反,我从主.jsp中包含的所有内容都包含许多行:

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?

推荐答案

根据 JSP规范


JSP.6.2 .3语义模型



...

JSP.6.2.3 Semantic Model

...

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

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.

所以,如果你想保留空格,你需要在< jsp:text> 中包装所需的部分。

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

这篇关于JSP Document / JSPX:什么决定了输出中如何删除制表符/空格/换行符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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