java JSP trimDirectiveWhitespaces

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25872068/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 08:49:44  来源:igfitidea点击:

JSP trimDirectiveWhitespaces

javajspservlets

提问by NinaNa

I am testing several ways to improve and reduce my response size, network traffic, and load time. One of the things I thought of using is the trimDirectiveWhitespaces element. I placed it in the page directive:

我正在测试几种方法来改进和减少我的响应大小、网络流量和加载时间。我想到使用的一件事是trimDirectiveWhitespaces 元素。我把它放在页面指令中:

<%@ page contentType="text/html;charset=UTF-8" language="java" trimDirectiveWhitespaces="true"%> 

I added some empty new lines and didn't see any change in my response or when i view source of the document, new lines were there.

我添加了一些空的新行,但在我的响应中没有看到任何变化,或者当我查看文档源时,出现了新行。

What is the correct usage of this and what am I doing wrong?

这个的正确用法是什么,我做错了什么?

回答by David Bala?ic

trimDirectiveWhitespacesonly removes empty lines around JSP tags and leaves other empty lines as they are.

trimDirectiveWhitespaces仅删除 JSP 标记周围的空行,而其他空行保持原样。

Example:

例子:

<% page pageEncoding="UTF-8"%>X
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>X
<html>


<head>

This will become:

这将变成:

X
X
<html>


<head>

(note I added Xto mark end-of-line for this example, so it is more obvious and stackoverflow wont trim them out)

(注意我X在这个例子中添加了标记行尾,所以它更明显并且stackoverflow不会修剪它们)

If trimDirectiveWhitespaces="true"is added to the pagedirective, the output becomes:

如果trimDirectiveWhitespaces="true"添加到page指令中,输出变为:

<html>


<head>

The first two empty lines are removed, but the next two (between the htmland headtags) are preserved.

前两个空行被删除,但接下来的两个(在htmlhead标签之间)被保留。

回答by SamDJava

You can place the following configuration for trim-directive-whitespacesin web.xmlas well:

你可以把下面的配置trim-directive-whitespacesweb.xml还有:

<web-app xmlns=...>
(...)
  <jsp-config>
    <jsp-property-group>
      <url-pattern>*.jsp</url-pattern>
      <trim-directive-whitespaces>true</trim-directive-whitespaces>
    </jsp-property-group>
  </jsp-config>
(...)
</web-app>