淺談如何刪除JSP編譯後的空行

來源:互聯網
上載者:User
    http://blog.csdn.net/st780206/archive/2010/03/12/5372736.aspx
    當你在用戶端用view source看JSP產生的程式碼時,會發現有很多空行,他們是由< %...% >後的斷行符號換行而產生的,也就是說每一行由< %...% >包含的JSP代碼到用戶端都變成一個空行,雖然不影響瀏覽,但還是希望能把他們刪掉。這裡將為大家介紹如何刪除JSP編譯後的空行。

    Tomcat刪除JSP編譯後的空行辦法如下:

    1. 支援JSP 2.1+ ,在每個要去空行的頁面裡包含下面代碼:

 
  1. <%@ page trimDirectiveWhitespaces="true" %> 

在 Tomcat 6.0.14下測試JSP編譯成功

2. 支援servlet 2.5+, 即 web.xml的 XSD版本為2.5,在web.xml中加入如下代碼

 
  1. <jsp-config> 
  2. <jsp-property-group> 
  3. <url-pattern>*.jsp</url-pattern> 
  4. <trim-directive-whitespaces>true</trim-directive-whitespaces> 
  5. </jsp-property-group> 
  6. </jsp-config> 

在tomcat 6.0.14下測試JSP編譯成功

3. Tomcat 5.5.x+,在Tomcat安裝目錄/conf/web.xml中找到名叫"jsp"的servlet,添加下面一段代碼:

 
  1. <init-param> 
  2. <param-name>trimSpaces</param-name> 
  3. <param-value>true</param-value> 
  4. </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:

 

 Hello world

 

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

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.