JSP 文档/JSPX:什么决定了如何在输出中删除制表符/空格/换行符?
我有一个格式良好的“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
根据 JSP 规范:
因此,如果您想保留空白,则需要将所需部分包装在
中。As per the JSP specification:
So, if you want to preserve whitespace, you need to wrap the desired parts in
<jsp:text>
.不是一个非常精确的答案,但根据此消息,Jasper 可能会这样做(我承认我没有检查自己):
该消息相当古老,但此评论中也报告了相同的行为你的问题似乎暗示它仍然适用。
不确定您是否可以更改此行为(不使用过滤器)。
Not a very precise answer but according to this message, Jasper might be doing this (I admit I didn't check myself):
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).
如果您在
web.xml
中使用上述指令,
内的空格将被去除。给定 BalusC 的回答,该指令仅在您不使用 XML 语法时才有用。(Tomcat 6.0.16;Servlet 2.5 声明;XML 语法中的 JSP 2.1 声明)
Whitespace inside
<jsp:text>
is stripped if you use the above directive in yourweb.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)