JSP trimSpaces 指令不起作用!

发布于 2024-10-28 19:30:58 字数 1291 浏览 1 评论 0 原文

我正在尝试使用 web.xml 中的trimSpaces 指令来修剪 JSP 空白,但它不起作用。这是我的 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-on-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>

我是新手,不知道如何调试问题。我尝试将 内标签,但这不起作用。也许 xml 模式有问题?或者我的JSP版本不正确? (我几个月前才安装了 Tomcat,所以它是最近的)

我使用 Tomcat 7。也使用 Spring,如您所见。

有什么建议吗?

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-on-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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

那片花海 2024-11-04 19:30:58

JspServlet 需要放入 servletcontainer 本身的 web.xml 中,不是你的网络应用程序的。转到 Tomcat 安装文件夹并打开 /conf/web.xml 文件。找到 JspServlet 条目并在其中添加

如果您想在 web 应用的 web.xml 中进行全局配置,例如因为 servletcontainer 配置不在您的控制范围内,那么您需要设置 的 -whitespaces> 属性。

<jsp-config>
    <jsp-property-group>
        <url-pattern>*.jsp</url-pattern>
        <trim-directive-whitespaces>true</trim-directive-whitespaces>
     </jsp-property-group>
</jsp-config>

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>
人生百味 2024-11-04 19:30:58

我将trimSpaces指令添加到tomcat/conf目录中的web.xml中,发现当我重新启动服务器时它被忽略了。然而,出于好奇,我还添加了一个 scrapdir 指令来覆盖默认工作目录,只是为了看看这个选项是否也被忽略了。当我添加此参数后重新启动 tomcat 时,scratchdir 和 trimSpace 指令都被选取并应用。我真的不明白为什么单独添加trimSpace指令不起作用,但与另一个指令一起添加它会导致它工作?这是一个有点笨拙的解决方案,但至少它有效。我使用的是 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>

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文