如何删除JSP编译后的空行

如何删除JSP编译后的空行

当你在客户端用view source看JSP生成的代码时,会发现有很多空行,他们是由< %...% >后的回车换行而生成的,也就是说每一行由< %...% >包含的JSP代码到客户端都变成一个空行,虽然不影响浏览,但还是希望能把他们删掉。这里将为大家介绍如何删除JSP编译后的空行。

 

1      Tomcat删除JSP编译后的空行办法如下:

1.      支持JSP 2.1+ ,在每个要去空行的页面里包含下面代码:

 

<%@ pagetrimDirectiveWhitespaces="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 templatetext between actions or directives be trimmed? [false]

在实际操作中我加入了5.5的配置到页面中并反复启动了几次tomcat但是还是没有成功,后来才想到JSP已经编译成servlet了所以没有能改变,进入到tomcat中的work目录把已经进行JSP编译的class全部删除,哇哈哈,整个世界清净了,成功删除空行

 

2      resin删除JSP编译后的空行办法

I'm getting a lot of whitespace in my JSPthat I don't intend to be there. Why is it appearing and how can I get rid ofit?

The extra whitespace is coming fromnewlines, 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.*' %>

Helloworld

 

Has newlines in these locations:

 

<%@page import='java.util.*' %>NL

<%@page import='java.io.*' %>NL

HelloworldNL

 

The result contains the newlines, which maybe surprising:

 

Hello world

 

One solution is to let the JSP tag extendacross the newline:

 

<%@page import='java.util.*'

%><%@page import='java.io.*'

%>Helloworld

 

Another solution is to use JSP comments toremove the newlines:

 

<%@page import='java.util.*' %><%--

--%><%@page import='java.io.*' %><%--

--%>Helloworld

 

Another solution is to use the XML syntaxof JSP. Parsing of XML causes removal of extra whitespace.

 

<jsp:root>

<jsp:directive.pageimport="java.util.*"/>

<jsp:directive.pageimport="java.io.*"/>

<jsp:text>Helloworld</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.*' %>/

Helloworld

 

3      Tomcat&Resin

tomcat

trimSpaces Should white spaces in templatetext between actions or directives be trimmed? [false]

 

http://tomcat.apache.org/tomcat-7.0-doc/jasper-howto.html

http://tomcat.apache.org/tomcat-6.0-doc/jasper-howto.html

http://tomcat.apache.org/tomcat-5.5-doc/jasper-howto.html

<servlet>

   ...

       <init-param>

           <param-name>trimSpaces</param-name>

           <param-value>true</param-value>

       </init-param>

   ...

</servlet>

 

resin

 

http://caucho.com/resin-4.0/reference.xtp

http://caucho.com/resin-3.1/doc/webapp-tags.xtp#webappdefault

<web-app-default>

...

         <jsp>

           ...

          <trim-directive-whitespaces>true</trim-directive-whitespaces>

         </jsp>

</web-app-default>


3.0.20 - Aug 1, 2006

JSP: preliminary trim-directive-whitespaces(#1204, rep by Bill Au)

3.1.9 (2009-04-13)

jsp: trimDirectiveWhitespaces was ignoredfor tag files (#3386, rep by kvirkki)


4      JSP Version Matrix for commonservers

Apache Tomcat 7.0.x

JSP 2.2

Apache Tomcat 6.0.x

JSP 2.1

Apache Tomcat 5.5.x

JSP 2.0

Apache Tomcat 4.1.x

JSP 1.2

Apache Tomcat 3.3.x

JSP 1.1

IBM WebSphere Application Server 7.x

JSP 2.x

IBM WebSphere Application Server 6.x

JSP 2.0

IBM WebSphere Application Server 5.x

JSP 1.2

 


参考:

http://blog.csdn.net/st780206/article/details/5372736
http://raibledesigns.com/rd/entry/trim_spaces_in_your_jsp
http://raibledesigns.com/rd/entry/trim_spaces_in_your_jsp1
http://www.giantgeek.com/blog/?p=348


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值