http://blog.csdn.net/st780206/archive/2010/03/12/5372736.aspx
當你在用戶端用view source看JSP產生的程式碼時,會發現有很多空行,他們是由< %...% >後的斷行符號換行而產生的,也就是說每一行由< %...% >包含的JSP代碼到用戶端都變成一個空行,雖然不影響瀏覽,但還是希望能把他們刪掉。這裡將為大家介紹如何刪除JSP編譯後的空行。
- <%@ page trimDirectiveWhitespaces="true" %>
在 Tomcat 6.0.14下測試JSP編譯成功
2. 支援servlet 2.5+, 即 web.xml的 XSD版本為2.5,在web.xml中加入如下代碼
- <jsp-config>
- <jsp-property-group>
- <url-pattern>*.jsp</url-pattern>
- <trim-directive-whitespaces>true</trim-directive-whitespaces>
- </jsp-property-group>
- </jsp-config>
在tomcat 6.0.14下測試JSP編譯成功
3. Tomcat 5.5.x+,在Tomcat安裝目錄/conf/web.xml中找到名叫"jsp"的servlet,添加下面一段代碼:
- <init-param>
- <param-name>trimSpaces</param-name>
- <param-value>true</param-value>
- </init-param>
本人沒測過,不過tomcat中web.xml檔案的協助這麼說的
trimSpaces Should white spaces in template text between actions or directives be trimmed? [false]
在實際操作中我加入了5.5的配置到頁面中並反覆啟動了幾次tomcat但是還是沒有成功,後來才想到JSP已經編譯成servlet了所以沒有能改變,進入到tomcat中的work目錄把已經進行JSP編譯的class全部刪除,哇哈哈,整個世界清淨了,成功刪除空行
resin刪除JSP編譯後的空行辦法如下:
I'm getting a lot of whitespace in my JSP that I don't intend to be there. Why is it appearing and how can I get rid of it?
The extra whitespace is coming from newlines, often at the end of declaration lines at the beginning of the JSP. For example, the following jsp:
<%@ page import='java.util.*' %><%@ page import='java.io.*' %>Hello world |
Has newlines in these locations:
<%@ page import='java.util.*' %>NL<%@ page import='java.io.*' %>NLHello worldNL |
The result contains the newlines, which may be surprising:
One solution is to let the JSP tag extend across the newline:
<%@ page import='java.util.*'%><%@ page import='java.io.*'%>Hello world |
Another solution is to use JSP comments to remove the newlines:
<%@ page import='java.util.*' %><%----%><%@ page import='java.io.*' %><%----%>Hello world |
Another solution is to use the XML syntax of JSP. Parsing of XML causes removal of extra whitespace.
<jsp:root><jsp:directive.page import="java.util.*"/><jsp:directive.page import="java.io.*"/><jsp:text>Hello world</jsp:text></jsp:root> |
Resin also supports the use of the '/' character to eat whitespace (this is a Resin specific feature):
<%@ page import='java.util.*' %>/<%@ page import='java.io.*' %>/Hello world |