问题
I have a Java servlet container using the Spring Framework. Pages are generated from JSPs using Spring to wire everything up. The resulting HTML sent to the user isn't as, well, tidy as I'd like. I'd like to send the HTML to Tidy right before it's sent to the client browser.
I'll set it up to work in development and be turned off in production; it's a winner, from my point of view, as it'll gain me more ease of maintenance.
Suggestions on how to make that work cleanly in Spring?
回答1:
Why do you want to do that? The best thing to do is to remove all whitespaces and compact the HTML as much as possible. The users see the rendered HTML, and mostly don't care about its structure and indentation. If you want the user to view the HTML he can use an HTML beautifier on the HTML on his machine.
More Info
JTidy has a servlet filter which you can apply to your jsps. Just add the jtidy jar to the WEB-INF/lib and the following lines to the web.xml:
<filter>
<filter-name>JTidyFilter</filter-name>
<filter-class>org.w3c.tidy.servlet.filter.JTidyFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>JTidyFilter</filter-name>
<servlet-name>DispatcherServlet</servlet-name>
</filter-mapping>
<filter-mapping>
<filter-name>JTidyFilter</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
回答2:
Haven't used myself but I don't think spring should be involved in this process at all, with this jtidy servlet extension should be enough for you.
来源:https://stackoverflow.com/questions/1589745/how-to-best-use-jtidy-with-a-spring-servlet-container