从jsp输出中删除空格

时间:2008-10-16 14:13:32

标签: jsp tomcat whitespace

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

9 个答案:

答案 0 :(得分:167)

有一个trimWhiteSpaces指令可以完成此任务,

在你的JSP中:

<%@ page trimDirectiveWhitespaces="true" %>

或者在你的web.xml的jsp-config部分中(请注意,这可以从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>

不幸的是,如果你有一个必需的空间,它可能也需要剥离,所以你可能需要在某些地方不间断的空间。

答案 1 :(得分:27)

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

{{1}}

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

答案 2 :(得分:4)

trimDirectiveWhitespaces仅受支持JSP 2.1及之后的servlet容器支持,或者在Tomcat 6或Tomcat 6的情况下(以及某些版本,例如Tomcat 6.0.10没有正确实现它 - 不知道其他的) )。 这里有关于trimDirectiveWhitespaces的更多信息:

http://www.oracle.com/technetwork/articles/javaee/jsp-21-136414.html

在这里

http://raibledesigns.com/rd/entry/trim_spaces_in_your_jsp1

答案 3 :(得分:3)

如果您正在使用标签,也可以在那里申请:

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

在你的jsp上:

<%@ page trimDirectiveWhitespaces="true" %>

答案 4 :(得分:2)

不是直接你要求的,但是有什么帮助我在我的jsp标签周围巧妙地放置HTML注释标签,并且还在servlet标签内放置空格(&lt; %%&gt;):

${"<!--"}
<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>

答案 5 :(得分:0)

使用

添加/编辑tomcat catalina.properties文件
org.apache.jasper.compiler.Parser.STRICT_QUOTE_ESCAPING=false

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

答案 6 :(得分:0)

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

E.g。改变:

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

答案 7 :(得分:0)

请使用修剪功能示例

fn:trim(string1)

答案 8 :(得分:0)

与实际问题略有出入,如果您想摆脱由于输出前所做的操作而造成的空白行,可以使用

out.clearBuffer();