我开始使用JSTL-spring。它很棒,但是我需要检查它生成的HTML代码。我有一个方法可以将所有的超文本标记语言代码附加到一个字符串中,所以当我对字符串执行<c:out>操作时,它会打印出一个很大的行,浏览器可以很好地解释它,但我不能,我需要这样做,以便准确地调试和生成干净的代码。
换句话说,我需要为HTML字符串换行,而不是为解释的html (不是<br>标记)换行。我试过/n,但它只打印/n。以下是代码示例:
productThumbnails+="<div class=\"prod_box\">";
productThumbnails+="<div class=\"top_prod_box\"></div>";
productThumbnails+="<div class=\"center_prod_box\">";
productThumbnails+="<div class=\"product_title\"><a href=\"details.html\">"+productTemp.getProductName()+"</a></div>";
productThumbnails+="<div class=\"product_title\"><a href=\"details.html\">"+productTemp.getProductModel()+"</a></div>";
productThumbnails+="<div class=\"product_img\"><a href=\"details.html\"><img width=\"90\" alt=\""+productTemp.getProductName()+"\" src=\""+productTemp.getProductImage()+"\"/></a></div>";
productThumbnails+="</div>";
productThumbnails+="</div>";在视图中,我使用以下命令打印所有内容:
<c:out value="${productThumbnails}" escapeXml="false" />发布于 2010-11-29 02:15:14
有两种方法:
Tomcat/conf/web.xml中的JspServlet条目。trimSpaces true
这样,标记库(JSTL等)留下的任何空格都将被修剪掉。这并不完美,但通常已经足够了。然而,这并不包括在错误的位置(即不是在JSP中)编写的超文本标记语言。
jTidyFilter。根据its documentation.
在/WEB-INF/lib中删除jtidyservlet.jar并在Webapp/WEB-INF/web.xml中声明过滤器
返回到评论,您处理这个问题是错误的:正确的方法是将List<Product>放在请求范围内(如果有必要,通过Spring bean),并使用JSTL <c:forEach>对其进行迭代。
<c:forEach items="${products}" var="product">
...
<div class="center_prod_box">
<div class="product_title"><a href="details.html"><c:out value="${product.productName}" /></a></div>
<div class="product_title"><a href="details.html"><c:out value="${product.productModel}" /></a></div>
<div class="product_img"><a href="details.html"><img width="90" alt="${product.productName}" src="${product.productImage}"/></a></div>
</div>
...
</c:forEach>这会产生更干净、更易维护的代码(以及HTML输出;)。
https://stackoverflow.com/questions/4298055
复制相似问题