views:

137

answers:

3

I have a Java project running on Glassfish that renders some ugly looking HTML. Its a side effect from using various internal and external JSP libraries. I would like to set up some sort of post-render filter that would feed the final HTML through HTMLTidy so that the source is nice and neat to aid debugging. Is this possible?

Is there a built in mechanism to perform some action after the server renders the JSPs into HTML? Can that action get the generated HTML as a string and manipulate it? Is there some easy built-in option to do this without extra coding?

+3  A: 

JTidyFilter

axtavt
Very cool. Thanks for the answer!!!
Freiheit
I'd also like to note that JTidyFilter is still a bit rough with no formal release. You must download it from the JTidy nightlies at: http://jtidy.sourceforge.net/nightly/
Freiheit
A: 

If you can alter control flow so that you get the html output before it's returned to the browser, then jtidy may help you.

I would view this as a worst-case fix though. In the long run, what should help more is to separate your html generating code and refactor that. Even in large, complex projects, you should be able to do this in small pieces and you'll get a gradual improvement. Otherwise, if the your problems grow to the point where tidy can't help, you'll be back where you started (and with even more unwieldily code to deal with).

Dana the Sane
A: 

Although JTidyFilter is extremely cool (haven't heard of it before, I upvoted the actual answer as well), I would just add that this behaviour can also be eliminated to a certain degree by just using the JSP 2.1 property trimDirectiveWhitespaces. This can be enabled in individual JSP files by:

<%@page trimDirectiveWhitespaces="true" %>

You can also apply this to all JSP files by the following entry in web.xml (which needs to be declared Servlet 2.5!):

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

This actually trims the whitespace left by taglibs and scriptlets. Also see this Sun article.

In pre JSP 2.1 servletcontainers or in JSP 2.1 servletcontainers which actually doesn't support this for some internal reasons, such as Tomcat, you just need to consult its JspServlet documentation for any initialization parameters. In for example Tomcat, you can configure it as well by setting trimSpaces init-param to true in for JspServlet in Tomcat's /conf/web.xml:

<init-param>
    <param-name>trimSpaces</param-name>
    <param-value>true</param-value>
</init-param>

Both approaches however doesn't "reformat" the HTML code, thus for example the following

 <ul>
     <c:forEach items="${list}" var="item">
         <li>${item}</li>
     </c:forEach>
 </ul>

would basically end up in

 <ul>
         <li>item1</li>
         <li>item2</li>
         <li>item3</li>
 </ul>

Thus with double indentation. You can actually workaround this by reformatting the code as

 <ul>
 <c:forEach items="${list}" var="item">
     <li>${item}</li>
 </c:forEach>
 </ul>

But I think the JTidyFilter is much better here :)

BalusC