Problemas com espaço em brancos nas minhas jsp[RESOLVIDO]

3 respostas
U

ola pessoal estou criando uma jsp(sitemap.jsp) que devolve conteudo xml para browser, portanto para funcionar adequadamente não deve ter nada antes da declaração documento xml.

meu codigo jsp:

<%@ page import="com.rl.common.url.*" %>
<%@ page contentType="text/xml" %>
<%
SiteMap map = new SiteMap();
String xml = map.generateSiteMap();
out.print(xml.trim());
%>

a classe gerada pelo tomcat(no dir …work):

try {
      _jspxFactory = JspFactory.getDefaultFactory();
      response.setContentType("text/xml;charset=ISO-8859-1");
      pageContext = _jspxFactory.getPageContext(this, request, response,
      			null, true, 8192, true);
      application = pageContext.getServletContext();
      config = pageContext.getServletConfig();
      session = pageContext.getSession();
      out = pageContext.getOut();
      _jspx_out = out;

      out.write("\r\n");
      out.write("\r\n");

SiteMap map = new SiteMap();
String xml = map.generateSiteMap();
out.print(xml.trim());

reparem o:

out.write("\r\n");
out.write("\r\n");

o generateSiteMap devolve uma string sem espaço !

porque gera isso ? como faço para retirar ?

o tomcat é 4.1.13

valeu

3 Respostas

sales_paulo

Ola,

Eu li o texto abaixo em um tuturial da Sun que estou estudando.
Nao testei ainda, mas acredito que vai te ajudar. Segue abaixo:

Eliminating Extra White Space

White space included in the template text of JSP pages is preserved by default. 
This can have undesirable effects. For example, a carriage return added after a taglib
directive would be added to the response output as an extra line.

If you want to eliminate the extra white space from the page, you can add a 
trim-directive-whitespaces element to a jsp-property-group element in the deployment
descriptor and set it to true.

To set the trim-directive-whitespaces element to true using NetBeans 5.5, do the following:

   1. Open the deployment descriptor fiile in the editor.
   2. Click the Pages button at the top of the editor.
   3. Select a JSP property group.
   4. Select the Trim Directive Whitespaces checkbox.
   5. Save the deployment descriptor.

Alternatively, a page author can set the value of the trimDirectiveWhitespaces attribute of the
page directive to true or false. This will override the value specified in the deployment
descriptor.

Custom tag authors can eliminate white space from the output generated by a tag file by
setting the trimDirectiveWhiteSpace attribute of the tag directive to true.

O link para o tuturial eh: http://java.sun.com/javaee/5/docs/tutorial/doc/index.html
Espero ter ajudado,
Paulo R. A. Sales.

cristianomariano

Já tive alguns problemas com isso.
Tente fazer isso, ou seja, tirar todos os espaços entre as tags:

<%@ page import="com.rl.common.url.*" %><%@ page contentType="text/xml" %><% SiteMap map = new SiteMap(); String xml = map.generateSiteMap(); out.print(xml.trim()); %>

U

cristianomariano:
Já tive alguns problemas com isso.
Tente fazer isso, ou seja, tirar todos os espaços entre as tags:

<%@ page import="com.rl.common.url.*" %><%@ page contentType="text/xml" %><% SiteMap map = new SiteMap(); String xml = map.generateSiteMap(); out.print(xml.trim()); %>

fica estranho o código mais funciona

Criado 13 de abril de 2007
Ultima resposta 13 de abr. de 2007
Respostas 3
Participantes 3