【问题标题】:html minification in AEM 6.0AEM 6.0 中的 html 缩小
【发布时间】:2017-09-11 09:43:28
【问题描述】:

我正在尝试使用 Maven https://mvnrepository.com/artifact/com.googlecode.htmlcompressor/htmlcompressor/1.4 中的 html 压缩器标签库来使用 HTML 缩小

我在 pom.xml 中添加了以下依赖项

<dependency>
    <groupId>com.googlecode.htmlcompressor</groupId>
    <artifactId>htmlcompressor</artifactId>
    <version>1.4</version>
</dependency>
<dependency>
    <groupId>com.yahoo.platform.yui</groupId>
    <artifactId>yuicompressor</artifactId>
    <version>2.4.6</version>
</dependency>

下面是我的 header.jsp

<%@include file="/apps/itchotelsonline/components/global.jsp"%>
<%@page import ="com.itc.hotels.services.ItcOnlineConfigurationService" %>
<%@ taglib uri="http://htmlcompressor.googlecode.com/taglib/compressor"
    prefix="compress"%>
<%
        ItcOnlineConfigurationService onlineConfigService = sling.getService(com.itc.hotels.services.ItcOnlineConfigurationService.class);
        if(onlineConfigService != null){
            String rootPagePath = onlineConfigService.getRootPagePath();
            slingRequest.setAttribute("rootPagePath",rootPagePath);
        }
%>
<compress:html removeIntertagSpaces="true" enabled="true" removeComments="false" compressJavaScript="true" yuiJsDisableOptimizations="true">
<cq:include path="${rootPagePath}/jcr:content/header" resourceType="itchotelsonline/components/content/header"/>

<div class="modal fade in new-modal" id="loginOverlayPopup" tabindex="-1" role="dialog" aria-labelledby="" aria-hidden="true" style="display: none;">
       <div class="modal-dialog">
           <!-- Modal content-->
           <div class="modal-content">

               <div class="modal-body text-center">
                   <center>
                       <p><strong id="loginInactivityPopUp"> You are Logged Out due to Inactivity! </strong></p>
                   </center>
                   <div class="clear"></div>
                   <div class="newBook clearfix">
                       <button type="button" class="btn btn-default" id="loginInactivityBtn" data-dismiss="modal" onclick="resetLoginInactivityPopUp();">Close</button>
                   </div>
               </div>
           </div>
       </div>
       </div>

我在其中添加了 compress html 行和 taglibs。

<%@ taglib uri="http://htmlcompressor.googlecode.com/taglib/compressor"
    prefix="compress"%>
<compress:html removeIntertagSpaces="true" enabled="true" removeComments="false" compressJavaScript="true" yuiJsDisableOptimizations="true">

下面是我的footer.jsp

<%@include file="/apps/itchotelsonline/components/global.jsp"%>
<%@page import ="com.itc.hotels.services.ItcOnlineConfigurationService" %>
<%
        ItcOnlineConfigurationService onlineConfigService = sling.getService(com.itc.hotels.services.ItcOnlineConfigurationService.class);
        if(onlineConfigService != null){
            String rootPagePath = onlineConfigService.getRootPagePath();
            pageContext.setAttribute("rootPagePath",rootPagePath);
        }
%>
<cq:include path="${rootPagePath}/jcr:content/footer" resourceType="itchotelsonline/components/content/footer"/>
</compress:html>

我在这结束了。

在给 maven build clean install 之后,我收到以下错误

org.apache.sling.scripting.jsp.jasper.JasperException: 绝对 uri:http://htmlcompressor.googlecode.com/taglib/compressor 无法在 web.xml 或随此应用程序部署的 jar 文件中解析 在 org.apache.sling.scripting.jsp.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:51)

我尝试重新启动 AEM 实例并刷新捆绑包,但仍未解决此问题。 我正在尝试在我的 AEM 网站上实现 HTML 缩小。我检查了 Day CQ HTML 库管理器,但它只压缩 CSS 和 JS,而不压缩通过 AEM 中的 JSP 呈现的 HTML。 AEM 中是否有可以使用的替代方法,或者可以使用 html 压缩器而不会出现上述错误? 请提前建议和感谢。

****新编辑******

我将库作为 OSGI 包导入,但现在它引发了类型转换异常。我发现 htmlcompressor 不压缩 jsps,但需要纯 html 进行压缩,页面速度模块是一个可能的替代方案,但 CSS 和 JS 已经被缩小了Day CQ HTML 库管理器,我们还通过更改 httpd.conf 中的缓存配置来利用浏览器缓存。我唯一的要求是真的缩小 HTML。是否有任何库可以与 AEM 集成并用于相同的用途?

【问题讨论】:

    标签: jsp jstl aem minify sling


    【解决方案1】:

    绝对 uri:http://htmlcompressor.googlecode.com/taglib/compressor 无法在 web.xml 或随此应用程序部署的 jar 文件中解析

    检查htmlcompressor 标签库是否存在于http://localhost:4502/system/console/status-jsptaglibs 中。看起来 HTML 压缩器 JAR 不是 OSGi 捆绑包,或者您没有将它正确导入到您的项目中。您不能只添加一个 Maven 依赖项并期望任何 JAR 在 OSGI 容器中工作,但我不会在这里介绍这一点,因为那里有很多信息。

    使用标签库压缩 HTML 对我来说听起来并不理想。在我看来,最好在位于 AEM 实例前面的 Web 服务器上安装像 Google mod_pagespeed 这样的模块。

    它将为您省去必须在代码中指定该标记的麻烦,并且还将对呈现的标记执行更彻底的优化(包括内联 Javascript 等)。

    【讨论】:

    • 我尝试使用 OSGI 包,但该库似乎不适用于 JSP。您能否建议一个替代库,它可以与 AEM 一起用于 HTML 缩小,并且可以与 JSP 一起使用,而不仅仅是 HTML?我正在查看颗粒 github.com/JonathanWalsh/Granule,但我如何将它集成到 AEM 中?
    • @AdityaAuradkar 对不起,我不知道图书馆会这样做,我不知道你为什么要这样做。当您的代码库重构为 Sightly 模板时会发生什么?仅供参考,您可以将 mod_pagespeed 配置为仅根据需要删除空格。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-13
    • 1970-01-01
    相关资源
    最近更新 更多