开发者

jsp:text losing whitespace inside tagx

Trying to use JSPX and running into a strange problem. Text inside jsp:text is supposed to not be subject to whitespace removal, according to the JSP documentation, but when I have a jsp:text element with leading whitespace in the beginning of a tagx file, when used in the jspx views, the leading whitespace disappears.

I've done quite a bit of Googling but can't find what's causing this to be removed. I've verified that the same jsp:text instances included directly in the jspx view work correctly, but put into a separate tagx file causes it to be lost.

This is under Jetty 6.1.19 and 6.1.22 using JSP 2.1 libraries.

EDIT: Some sample code follows. Note that the spaces leading and trailing within the <jsp:text> tags are stripped. Judging by the JSP documentation I can see, whitespace within those should be retained.

WEB-INF/tabs/nameValuePair.tagx:

<jsp:root version="2.0" xmlns:jsp="http://java.sun.com/JSP/Page">
<jsp:directive.attribute name="name" type="java.lang.String" required="true"/>
<jsp:directive.attribute name="value" type="java.lang.String" required="true"/>
<jsp:text> ${name}=${value} </jsp:text>
</jsp:root>

test.jspx:

<jsp:root version="2.0" xmlns:jsp="http://java.sun.com/JSP/Page" xmlns:t="urn:jsptagdir:/WEB-INF/tags">
<jsp:directive.page contentType="text/html; charset=ISO-8859-1" />

<html>
    <head>
        <title>Test</title>
    </head>
    <body>
        <t:nameValuePair name="name" value="Google" />
        <t:nameValuePair name="age" value="11" />
        <t:nameValuePair name="description" value="Popular Search Engine" />
        <jsp:text> test=value </jsp:text>
    </body>
</html>

</jsp:root>

outpu开发者_运维技巧t:

<html><head><title>Test</title></head><body>name=Googleage=11description=Popular Search Engine test=value </body></html>


It looks to me like the tagx is being trimmed to make it simpler to write the tags. A new line in the end would for instance end up with a space where the tag is used and that might be bad.

Though why not put your text in the tag in a span:

<jsp:text><span> ${name}=${value} </span></jsp:text>

Or use the non breaking space: &nbsp;

This way you avoid the trim and you should get the result you want.


I had this problem with loosing whitespace, too. I am using jspx Documents and a pre compiler for production. But in my development machine I am using an embedded tomcat to recompile the jspx docs on the fly whenever the jsp is changing.

First I needed to achieve the same compilation results with the precompiler and the embedded tomcat compiler with this snippet for my web.xml

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

With this directive activated both behave the same. If you try to preserve whitespace don't do it like this:

<span class="ex1"> </span>
<span class="ex2"><jsp:text> </jsp:text></span>
<span class="ex3"> <jsp:text><!-- keep --> </jsp:text></span>
<span class="ex4"><!--  keep --></span>
<span class="ex5"><span> </span> <!--  keep --></span>
<span class="ex6">&#032;</span>
<span class="ex7">&#009;</span>

These are the only options which work for me:

<span class="ex8">&#160;</span>
<span class="ex9"><c:out value=" " /></span>

The Resulting jsp Compilation looks like this:

out.write("<span class=\"ex1\"/>");
out.write("<span class=\"ex2\">");
out.write("</span>");
out.write("<span class=\"ex3\">");
out.write("</span>");
out.write("<span class=\"ex4\">");
out.write("</span>");
out.write("<span class=\"ex5\">");
out.write("<span/>");
out.write("</span>");
out.write("<span class=\"ex6\"/>");
out.write("<span class=\"ex7\"/>");
out.write("<span class=\"ex8\">");
out.write(' ');
out.write("</span>");
out.write("<span class=\"ex9\">");
if (_jspx_meth_c_005fout_005f0(_jspx_th_ktr_005fform_005f0, _jspx_page_context))
  return true;
out.write("</span>");

By the way. You see the reference   which is a non-breaking space in XML which results in out.write(' '); in the compiled jsp. But the non-breaking space is preservered even if you don't see it. If you do a hexdump -C test.jspx you get:

6f 75 74 2e 77 72 69 74  65 28 27 c2 a0 27 29 3b  |out.write('..');|

".." in the right columns shows the non printable characters "c2 a0" which is unicode for "non breaking space".

As a non breaking space is not always what you want, your only option is <c: out value=""/>. If there are other options, I am happy to hear about it in the comments.


My text still had to break, so i added <wbr> before the spaces between the ${...}s.

Turns out that doesn't work in Internet Explorer, and the problem was this line:

<%@ page trimDirectiveWhitespaces="true" %>

This fixed it:

<%@ page trimDirectiveWhitespaces="false" %>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜