I'm trying to trim JSP whitespace using the trimSpaces directive in my web.xml, but it's not working. Here's my web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_4.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
id="WebApp_ID"
version="2.4">
<display-name>MyWebApp</display-name>
<servlet>
<servlet-name>MyServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-o开发者_如何学编程n-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
<init-param>
<param-name>trimSpaces</param-name>
<param-value>true</param-value>
</init-param>
</web-app>
I'm new at this and not sure how to go about debugging the problem. I tried putting the <init-param> inside the <servlet> tags, but that didn't work. Maybe there's something wrong with the xml schema? Or maybe my JSP version isn't correct? (I only installed Tomcat a few months ago, so it's pretty recent)
I'm using Tomcat 7. Also using Spring, as you can see.
Any suggestions?
The <init-param>
for the JspServlet
needs to go in the web.xml
of the servletcontainer itself, not of your webapp. Go to the Tomcat installation folder and open the /conf/web.xml
file. Locate the <servlet>
entry of the JspServlet
and add the <init-param>
there.
If you'd like to configure it globally in the web.xml
of your webapp, for example because the servletcontainer configuration is outside your control, then you need to set the <trim-directive-whitespaces>
property of the <jsp-config>
instead.
<jsp-config>
<jsp-property-group>
<url-pattern>*.jsp</url-pattern>
<trim-directive-whitespaces>true</trim-directive-whitespaces>
</jsp-property-group>
</jsp-config>
I added the trimSpaces directive to the web.xml in my tomcat/conf directory and found that it was ignored when I restarted my server. However, out of curiosity I also added a scratchdir directive to override the default work directory, just to see if this option was also ignored. When I restarted tomcat after adding this param, both the scratchdir and trimSpace directives were picked up and applied. I don't really understand why adding the trimSpace directive on its own takes not effect yet adding it in conjunction with another directive causes it to work? It's a bit of a clunky solution but at least it works. I'm using tomcat 6.0.33.
<servlet>
<servlet-name>jsp</servlet-name>
<servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
<!-- trim white space in jsp -->
<init-param>
<param-name>trimSpaces</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>scratchdir</param-name>
<param-value>/temp/tomcat/work</param-value>
</init-param>
<!-- these params are here by default -->
<init-param>
<param-name>fork</param-name>
<param-value>false</param-value>
</init-param>
<init-param>
<param-name>xpoweredBy</param-name>
<param-value>false</param-value>
</init-param>
<load-on-startup>3</load-on-startup>
</servlet>
精彩评论