jsp压缩html,使用HtmlCompressor压缩JSP编译的Html代码

朱阳曜
2023-12-01

HtmlCompressor 能够删除多余的HTML代码。它提供多种方法:删除无用的空行、删除注释以及删除无用的表格等等,简单而有效。

在Java代码中可以这样使用:

String html = getHtml(); //需要处理的Html代码

HtmlCompressor compressor = new HtmlCompressor();

String compressedHtml = compressor.compress(html);

相关属性说明:

HtmlCompressor compressor = new HtmlCompressor();

compressor.setEnabled(true); //if false all compression is off (default is true)

compressor.setRemoveComments(true); //if false keeps HTML comments (default is true)

compressor.setRemoveMultiSpaces(true); //if false keeps multiple whitespace characters (default is true)

compressor.setRemoveIntertagSpaces(true);//removes iter-tag whitespace characters

compressor.setRemoveQuotes(true); //removes unnecessary tag attribute quotes

compressor.setCompressCss(true); //compress css using Yahoo YUI Compressor

compressor.setCompressJavaScript(true); //compress js using Yahoo YUI Compressor

compressor.setYuiCssLineBreak(80); //--line-break param for Yahoo YUI Compressor

compressor.setYuiJsDisableOptimizations(true); //--disable-optimizations param for Yahoo YUI Compressor

compressor.setYuiJsLineBreak(-1); //--line-break param for Yahoo YUI Compressor

compressor.setYuiJsNoMunge(true); //--nomunge param for Yahoo YUI Compressor

compressor.setYuiJsPreserveAllSemiColons(true);//--preserve-semi param for Yahoo YUI Compressor

String compressedHtml = compressor.compress(html);

也可以通过Taglib方式在JSP页面上使用:

需将htmlcompressor.jar放到项目的 lib/ 目录中

在你的JSP里添加如下代码:

必需在JSP 2.0以上版本

将内容放在压缩标签内:

...

 类似资料: