在jsp编译成java之后发现在java文件中有很多 out.write("\r\n"),经过调查测试认为是jsp中属于非页面输出的行都以这种方式被编译了,请问有什么方法能够去掉这些 out.write("\r\n")?我用的是MyEclipse5.1.1 Tomcat5.5服务器 struts2基盘 
因为我的JSP文件很大,如果打出的空行太多有可能会导致文件超长崩溃 
修改编译后的java文件也不太可行, 
我不知道怎么能将自己改的java文件编译成class, 
由于不是独立的java文件,所以控制台编译会提示很多错误

解决方案 »

  1.   

    不是独立的文件也可以把Java文件编译成class文件,在cmd命令里面搞定
      

  2.   

    把tomcat下的lib包加到classpath下试一下
    我在开发环境下把tomcat下lib包加到路径下
    可以编译index_jsp.java
    package org.apache.jsp;import javax.servlet.*;
    import javax.servlet.http.*;
    import javax.servlet.jsp.*;public final class index_jsp extends org.apache.jasper.runtime.HttpJspBase
        implements org.apache.jasper.runtime.JspSourceDependent {  private static final JspFactory _jspxFactory = JspFactory.getDefaultFactory();  private static java.util.List _jspx_dependants;  private javax.el.ExpressionFactory _el_expressionfactory;
      private org.apache.AnnotationProcessor _jsp_annotationprocessor;  public Object getDependants() {
        return _jspx_dependants;
      }  public void _jspInit() {
        _el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();
        _jsp_annotationprocessor = (org.apache.AnnotationProcessor) getServletConfig().getServletContext().getAttribute(org.apache.AnnotationProcessor.class.getName());
      }  public void _jspDestroy() {
      }  public void _jspService(HttpServletRequest request, HttpServletResponse response)
            throws java.io.IOException, ServletException {    PageContext pageContext = null;
        HttpSession session = null;
        ServletContext application = null;
        ServletConfig config = null;
        JspWriter out = null;
        Object page = this;
        JspWriter _jspx_out = null;
        PageContext _jspx_page_context = null;
        try {
          response.setContentType("text/html; charset=GB2312");
          pageContext = _jspxFactory.getPageContext(this, request, response,
           null, true, 8192, true);
          _jspx_page_context = pageContext;
          application = pageContext.getServletContext();
          config = pageContext.getServletConfig();
          session = pageContext.getSession();
          out = pageContext.getOut();
          _jspx_out = out;      out.write("\r\n");
          out.write(" \r\n");
          out.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\r\n");
          out.write("<html>\r\n");
          out.write("<head>\r\n");
          out.write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\">\r\n");
          out.write("<title>Insert title here</title>\r\n");
          out.write("</head>\r\n");
          out.write("<body>\r\n");
          out.write("<form method=\"post\" name=\"helloForm\" action=\"HelloWorldAction.do\">\r\n");
          out.write(" <hr>谁想说<hr>\r\n");
          out.write("<br>请输入想说话的人的名字:<input type=\"text\" name=\"username\" value=\"\"><br>\r\n");
          out.write("<input type=\"submit\" name=\"submit\" value=\"提交\">\r\n");
          out.write("</form>\r\n");
          out.write(" \r\n");
          out.write("</body>\r\n");
          out.write("</html>");
        } catch (Throwable t) {
          if (!(t instanceof SkipPageException)){
            out = _jspx_out;
            if (out != null && out.getBufferSize() != 0)
              try { out.clearBuffer(); } catch (java.io.IOException e) {}
            if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);
          }
        } finally {
          _jspxFactory.releasePageContext(_jspx_page_context);
        }
      }
    }
      

  3.   

    楼上的办法好像行不通,我的jsp中用的基本都是自定义标签,引用了很多包,包括外部引用的,这样做就算可以编译也会很麻烦,每次对jsp有改动都要重新编译java文件,还要自己手动改,手动编译class能不能通过其它方法实现这个要求,比如在编译器上做文章,因为我需要做的很简单,就是把out.write("\r\n")去掉,是不是可以在编译的时候设置不编译非输出的行呢
      

  4.   

    如果楼主的是Tomcat5的话,利用web服务器的trimSpaces功能应该可以解决。 
    在server.xml中定义
    <servlet> 
    <servlet-name>jsp</servlet-name> 
    <servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class> 
    <init-param> 
    <param-name>fork</param-name> 
    <param-value>false</param-value> 
    </init-param> 
    <init-param> 
    <param-name>trimSpaces </param-name> 
    <param-value>true </param-value> 
    </init-param> 
    <init-param> 
    <param-name>xpoweredBy</param-name> 
    <param-value>false</param-value> 
    </init-param> 
    <load-on-startup>3</load-on-startup> 
    </servlet> 
    这个方案有个缺点,它会去掉所有jsp EL标签之间的空格的换行符,在部分情况下也来带来不便。 
    如:Your name is ${firstName} ${lastName}. ==输出为==> Your name is firstNamelastName. 
    两个${}变量之间的空格也会消失。要解决这个问题是相当麻烦,要引入一个只有一个空格的变量。 
    <c:set var="one_space"> </c:set> 
    Your name is ${firstName}${one_space}${lastName}. 
    还有个方法就是在jsp中加入
    <%@ page trimDirectiveWhitespaces="true" %> 
    这个方案只有在支持jsp 2.1的web服务器上才可以使用,如Tomcat6。