I've got a pretty basic Spring MVC application that I created using Spring Roo. So the whole project is set up as a standard Spring/Tiles application (scaffolding created by Roo as well). When I look at the generated HTML source code, I see that all the whitespaces/linebreaks have been trimmed. For debugging purposes, this is difficult/problematic for me.

I'm using Spring 3.x with Tomcat 7.

I've searched around SO and the web and found that there is a trimWhiteSpace jsp directive (example at this SO post), but when I search the entire Roo generated codebase, I do not see this directive anywhere.

I've even gone so far as adding the <init-param> and the <jsp-config> lines to the web.xml specifying false, but that has made no difference either.

At this point, I am not sure if it is Tomcat that is stripping the spacing or if it is something in Spring/Tiles that is doing the deed.

How can I disable this feature and have all the spacing/linebreaks outputted in my HTML that exist in my tag definitions and my jsp files?

Example of generated HTML:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE HTML SYSTEM "about:legacy-compat">
<html><head><meta content="text/html; charset=UTF-8" http-equiv="Content-Type"/><meta content="IE=8" http-equiv="X-UA-Compatible"/><link href="/V2/resources/dijit/themes/tundra/tundra.css" type="text/css" rel="stylesheet"/><link href="/V2/resources/styles/standard.css" media="screen" type="text/css" rel="stylesheet"/><link href="/V2/resources/images/favicon.ico" rel="SHORTCUT ICON"/><script type="text/javascript">var djConfig = {parseOnLoad: false, isDebug: false, locale: 'en-us'};</script><script type="text/javascript" src="/V2/resources/dojo/dojo.js"></script><script type="text/javascript" src="/V2/resources/spring/Spring.js"></script><script type="text/javascript" src="/V2/resources/spring/Spring-Dojo.js"></script><script type="text/javascript" language="JavaScript">dojo.require("dojo.parser");</script><script type="text/javascript" src="/V2/resources/jQuery/jquery-2.0.3.min.js"></script><link href="/V2/resources/jQWidgets-3.0.2/styles/jqx.darkblue.css" type="text/css" rel="stylesheet"/><script type="text/javascript" src="/V2/resources/jQWidgets-3.0.2/jqxcore.js"></script><title>Welcome to V2</title></head><body class="tundra spring"><div id="wrapper"><div version="2.0" id="header"><a title="Home" name="Home" href="/V2/"><img src="/V2/resources/images/banner-graphic.png"/></a></div><div id="main"><div version="2.0"><script type="text/javascript">dojo.require('dijit.TitlePane');</script><div id="_title_title_id"><script type="text/javascript">Spring.addDecoration(new Spring.ElementDecoration({elementId : '_title_title_id', widgetType : 'dijit.TitlePane', widgetAttrs : {title: 'Internal Error', open: true}})); </script><h2>Internal Error</h2><p>Sorry, we encountered an internal error.</p></div></div><div version="2.0" id="footer"><span><a href="/V2/">Home</a></span><span id="language"> | Language: <a title="Switch language to English" href="?lang=en"><img alt="Switch language to English" src="/V2/resources/images/en.png" class="flag"/></a> </span><span> | Theme: <a title="standard" href="?theme=standard">standard</a> | <a title="alt" href="?theme=alt">alt</a></span><span><a title="Sponsored by SpringSource" href="http://springsource.com"><img src="/V2/resources/images/springsource-logo.png" alt="Sponsored by SpringSource" align="right"/></a></span></div></div></div></body></html>

Example of default.jspx (base tiles layout):

<html xmlns:jsp="http://java.sun.com/JSP/Page" xmlns:c="http://java.sun.com/jsp/jstl/core" xmlns:tiles="http://tiles.apache.org/tags-tiles" xmlns:spring="http://www.springframework.org/tags" xmlns:util="urn:jsptagdir:/WEB-INF/tags/util" >  

    <jsp:output doctype-root-element="HTML" doctype-system="about:legacy-compat" />

    <jsp:directive.page contentType="text/html;charset=UTF-8" />  
    <jsp:directive.page pageEncoding="UTF-8" /> 

    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=8" />    

        <util:load-scripts />

        <spring:message code="application_name" var="app_name" htmlEscape="false"/>
        <title><spring:message code="welcome_h3" arguments="${app_name}" /></title>
    </head>

    <body class="tundra spring">
        <div id="wrapper">
            <tiles:insertAttribute name="header" ignore="true" />
            <tiles:insertAttribute name="menu" ignore="true" />   
            <div id="main">
                <tiles:insertAttribute name="body"/> 
                <tiles:insertAttribute name="footer" ignore="true"/>
            </div>
        </div>
    </body>
</html>

As you can see, all the whitespaces/linebreaks from the tiles layout definitions have been stripped out.

有帮助吗?

解决方案

So, after a lot of painful hair pulling (and a few links that pushed me in the right direction), it turns out that the whitespace trimming isn't from the container, but rather from the JSPX parser. Given that they are XML files, the parser is loading up the entire DOM structure and then dumping it out. In building the DOM, it obviously loses and spacing information between the tags. Apparnetly (I haven't tested) the only way to retain spacing between the tags is to enclose everything in <jsp:text> blocks (see here).

On the other hand, I just gave up and converted everything to JSP files and everything works fine. Easier for me to deal with at the moment.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top