ChatGPT解决这个技术问题 Extra ChatGPT

从 jsp 输出中去除空格

如何从 jsp 页面的输出中去除多余的空格?有没有可以在我的 web.xml 上翻转的开关?是否有特定于 Tomcat 的设置?


j
jseidl

有一个 trimWhiteSpaces 指令可以完成这个,

在您的 JSP 中:

<%@ page trimDirectiveWhitespaces="true" %>

或者在 jsp-config 部分中您的 web.xml (请注意,这从 servlet 规范 2.5 开始。):

<jsp-config>
  <jsp-property-group>
    <url-pattern>*.jsp</url-pattern>
    <trim-directive-whitespaces>true</trim-directive-whitespaces>
  </jsp-property-group>
</jsp-config>

不幸的是,如果您有所需的空间,它可能还需要剥离它,因此您可能在某些位置需要一个不间断的空间。


这两个选项在性能方面有区别吗?
trimDirectiveWhitespaces 仅受支持 JSP 2.1 及更高版本的 servlet 容器支持,或者在 Tomcat、Tomcat 6 的情况下(以及某些版本,例如 Tomcat 6.0.10 没有正确实现它 - 不知道其他版本),有有关 trimDirectiveWhitespaces 的更多信息,请点击此处:java.sun.com/developer/technicalArticles/J2EE/jsp_21 和此处raibledesigns.com/rd/entry/trim_spaces_in_your_jsp1
在 JSP 自定义 .tag 文件中,使用 <%@ tag body-content="scriptless" trimDirectiveWhitespaces="true" %>。
Tomcat 7 web.xml:<init-param> <param-name>trimSpaces</param-name> <参数值>true</参数值> </初始化参数>
我正在运行 Tomcat 8,这似乎不起作用。它对任何人都有效吗?
B
BalusC

如果您的 servletcontainer 不支持 JSP 2.1 trimDirectiveWhitespaces 属性,那么您需要查阅其 JspServlet 文档以了解任何初始化参数。例如在 Tomcat 中,您也可以通过在 Tomcat 的 /conf/web.xml 中为 JspServlet 设置 trimSpaces init-param 为 true 来配置它:

<init-param>
    <param-name>trimSpaces</param-name>
    <param-value>true</param-value>
</init-param>

一个完全不同的替代方案是 JTidyFilter。它不仅修剪空白,而且还以正确的缩进格式化 HTML。


我已通过在 /conf/web.xml 中将 trimSpaces init-param 设置为 true 进行配置,但生成的 html 中的空格未修剪。我正在使用 Tomcat 6.0。有任何想法吗?
@ria:请注意,您需要了解这只会修剪 JSTL 和 scriptlet 等标记库留下的空白。如果您想从 HTML 中删除所有空格,请寻求不同的解决方案。您可以在此处找到过滤器示例:balusc.blogspot.com/2007/12/whitespacefilter.html
我刚刚注意到我网站上的 b/c 文章中的代码出现了一些问题,开始出现错误。我复制了 html 源代码并将其粘贴到 w3 验证器中,以发现空白过滤器偶尔会连接属性。例如这个...
r
redolent

不是您直接要求的,而是帮助我以巧妙的方式在我的 jsp 标记周围放置 HTML 注释标记,并将空格放在 servlet 标记 (<% %>) 中:

${"<!--"}
<c:if test="${first}">
    <c:set var="extraClass" value="${extraClass} firstRadio"/>
</c:if>
<c:set var="first" value="${false}"/>
${"-->"}<%

%><input type="radio" id="input1" name="dayChooser" value="Tuesday"/><%
%><label for="input1" class="${extraClass}">Tuesday</label>

A
Andres

如果您使用标签,您也可以在那里应用:

<%@ tag description="My Tag" trimDirectiveWhitespaces="true" %>

在你的jsp上:

<%@ page trimDirectiveWhitespaces="true" %>

y
yglodt

您可以更进一步,还可以在构建时删除 html 标记之间的换行符(回车)。

例如改变:

<p>Hello</p>
<p>How are you?</p>

进入:

<p>Hello</p><p>How are you?</p>

这样做,使用 maven-replacer-plugin 并在 pom.xml 中进行设置:

<plugin>
    <groupId>com.google.code.maven-replacer-plugin</groupId>
    <artifactId>replacer</artifactId>
    <version>1.5.3</version>
    <executions>
        <execution>
            <id>stripNewlines</id>
            <phase>prepare-package</phase>
            <goals>
                <goal>replace</goal>
            </goals>
            <configuration>
                <basedir>${project.build.directory}</basedir>
                <filesToInclude>projectname/WEB-INF/jsp/**/*.jsp</filesToInclude>
                <token>&gt;\s*&lt;</token>
                <value>&gt;&lt;</value>
                <regexFlags>
                    <regexFlag>MULTILINE</regexFlag>
                </regexFlags>
            </configuration>
        </execution>
    </executions>
</plugin>

这只会修改 build-directory 中的 JSP,而不会触及源代码中的 JSP。

您可能需要调整 JSP 所在的路径 (<filesToInclude>)。


J
Jorge Santos Neill

请使用修剪功能,例如

fn:trim(string1)

E
Earlz

添加/编辑您的 tomcat catalina.properties 文件

org.apache.jasper.compiler.Parser.STRICT_QUOTE_ESCAPING=false

另请参阅:https://confluence.sakaiproject.org/display/BOOT/Install+Tomcat+7


G
Ghostff

离实际问题有点远,如果你想摆脱你在输出之前所做的任何事情导致的空行,你可以使用

out.clearBuffer();