这将是一个HTML缩小器,可以跳过<%和之间的所有内容%>.
实际上,开源HTML缩小器将是一个很好的起点,特别是如果它已经有代码来保存某些块的内容<textarea.它的代码也可以用来保存<%%>块.
我知道HTML minifiers不太常见,因为它比JS/CSS更频繁地更改并且通常是动态生成的,但如果在编译缓存副本之前可以使JSP编译器缩小,则会导致缩小HTML.
另外,ASP minifier可能非常接近同样的事情.而且我不关心对服务器有意义的自定义标签.对服务器(对我的公司而言)重要的唯一事情是在<%%>块中.
Nic*_*son 11
这个问题有点过时,但资源的答案仍然没有成为发布的方式.
HtmlCompressor使这件事成为可能而且非常简单.
您可以通过Java API使用它:
String html = getHtml(); //your external method to get html from memory, file, url etc.
HtmlCompressor compressor = new HtmlCompressor();
String compressedHtml = compressor.compress(html);
Run Code Online (Sandbox Code Playgroud)
或者您可以通过Taglib使用它:
Download .jar file of the current release and put it into your lib/ directory
Add the following taglib directive to your JSP pages:
<%@ taglib uri="http://htmlcompressor.googlecode.com/taglib/compressor" prefix="compress" %>
Please note that JSP 2.0 or above is required.
Run Code Online (Sandbox Code Playgroud)
在JSP中:
<compress:html removeIntertagSpaces="true">
<!DOCTYPE html>
...
</html>
</compress:html>
Run Code Online (Sandbox Code Playgroud)
干杯
JSP转换为 Java 代码,随后编译为字节码。那么缩小 JSP 就没有意义了。
您可以通过编写自定义过滤器来处理JSP 页面生成的输出。我已经编写了过滤器来修剪 JSP 输出中的空行和不必要的空格,不幸的是它不是公开的。但如果您四处搜索,我相信您可以找到 servlet 过滤器来从生成的 HTML 中删除不需要的内容。
看一下修剪过滤器 ( http://www.servletsuite.com/servlets/trimflt.htm ),您可以简单地将其映射到 web.xml 中。它将帮助您删除空格,还可以删除注释。
根据我的经验,如果您使用本身没有任何输出的标签,例如 JSTL C 控制标签(c:if、c:choose、...),JSP 中会出现大量空白,然后这会非常严重。便利。