Question

I'm trying to test the Jtidy filter on a ridiculously simple hello world Struts project.

I'm following other answers that were given here in the past.

I do not get any errors during deployment or accessing JSPs. But it seems like the filter does nothing. i.e. no html comments are being removed from the created html's.

The Struts filter seems to work fine.. since my Action class is getting called.

Here is my web.xml:

<web-app id="WebApp_ID" version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

  <display-name>Struts 2 Web Application</display-name>

  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>

    <filter>
        <filter-name>JTidyFilter</filter-name>
        <filter-class>org.w3c.tidy.servlet.filter.JTidyFilter</filter-class>

        <init-param>
            <param-name>config</param-name>
            <param-value>indent: auto; indent-spaces: 2; hide-comments: true</param-value>
        </init-param>
    </filter>

  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

    <filter-mapping>
        <filter-name>JTidyFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

</web-app>

What might I be doing wrong here? How can I find out, if I don't get any exceptions?

Was it helpful?

Solution

First, I had to change the filter order of execution, so that the Jtidy filter will be called last (after the Struts filter has completed its work):

The new web.xml:

<web-app id="WebApp_ID" version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

  <display-name>Struts 2 Web Application</display-name>

    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher </filter-class>
    </filter>

    <filter>
        <filter-name>JTidyFilter</filter-name>
        <filter-class>org.w3c.tidy.servlet.filter.JTidyFilter</filter-class>

        <init-param>
            <param-name>config</param-name>
            <param-value>indent: auto; indent-spaces: 2; hide-comments: true</param-value>
        </init-param>
    </filter>

    <filter-mapping>
        <filter-name>JTidyFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

</web-app>

Second, I had to have on the classpath not only the jtidyservlet jar, but also the jtidy jar (without the latter, compilation went fine but I would get a java.lang.ClassNotFoundException: org.w3c.tidy.TidyMessageListener in runtime).

in my pom.xml:

<dependency>
    <groupId>jtidy</groupId>
    <artifactId>jtidyservlet</artifactId>
    <version>r8-SNAPSHOT</version>
</dependency>

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>jtidy</artifactId>
    <version>r8-21122004</version>
</dependency>

I also had to add the jtidyservlet jar manually to my local maven repo:

  1. Download jtidyservlet-r8-SNAPSHOT.jar from: http://jtidy.sourceforge.net/nightly/

  2. Drop it in C:\TEMP

  3. Run from command line:

SET LOCAL_JARS=C:\TEMP

mvn install:install-file -DgroupId=jtidy -DartifactId=jtidyservlet -Dversion=r8-SNAPSHOT -Dpackaging=jar -Dfile=%LOCAL_JARS%\jtidyservlet-r8-SNAPSHOT.jar -DgeneratePom=true

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top