【问题标题】:How to fix RichFaces' resources being included multiple times with JBoss EAP 7.3.0?如何修复在 JBoss EAP 7.3.0 中多次包含 RichFaces 的资源?
【发布时间】:2020-08-22 08:25:22
【问题描述】:

我目前正在从 JBoss EAP 7.1 迁移到 7.3,我的一个问题与 RichFaces 相关 - 这是 v4.5.17 的 EOL 非常很长一段时间——我知道。但是,我现在需要处理这个问题。

不知何故,RichFaces 的资源现在多次包含在生成的 HTML 标记中,这显着延迟了 UI 的加载。浏览器现在必须处理多个不必要的请求,这在加载页面时需要几秒钟。

除了完全放弃 RichFaces 之外,还有任何已知的解决方案吗?

到目前为止我找不到任何东西......

【问题讨论】:

  • 我假设您已经检查过是否多次包含 RichFaces jar。或者在没有 PrimeFaces 的页面上尝试或检查多个 h:head 或...
  • 当然...这是通过 JSF 上下文中的使用来处理的(<ui:composition> 带有使用的组件)——而不是手动处理。我要从中迁移的 EAP 7.1.6 不会发生这种情况。
  • 包含的数量似乎与每页使用的组件数量有关。
  • ...虽然不是 1:1...单个 <rich:calendar> 已经触发了 10 个包含。但使用的组件越多,数量就越高。
  • stackoverflow.com/questions/23824219/…(看起来 Richfaces 做了一些奇怪的事情,在较新的 JSF 版本中可能不再适用)

标签: jsf richfaces jboss-eap-7


【解决方案1】:

升级到 JSF 2.3.9 后,我注意到我的“旧”应用程序出现了相同的行为。

使用 Richfaces 4.5.17.Final 似乎足以停用资源优化

(在 web.xml 中)

<context-param>
    <param-name>org.richfaces.resourceOptimization.enabled</param-name>
    <param-value>false</param-value>
</context-param>
  

【讨论】:

    【解决方案2】:

    我已经实现了一个 Servlet 过滤器 来修复生成的 HTML 输出,然后再将其移交给客户端。它完全删除所有重复 &lt;link&gt; 和 &lt;script&gt; 元素 - 不仅限于这些特定的 RichFaces 嵌入。

    public class RichFacesDuplicatesFilter implements Filter {
    
        @Override
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    
            // Ignore resources...
            final String path = ((HttpServletRequest) request).getServletPath();
            if (path.startsWith("/javax.faces.resource")) {
                chain.doFilter(request, response);
                return;
            }
    
            // Wrapper via https://stackoverflow.com/a/23381235
            CapturingResponseWrapper wrapper = new CapturingResponseWrapper((HttpServletResponse) response);
            chain.doFilter(request, wrapper);
    
            String content = wrapper.getCaptureAsString();
            content = this.removeDuplicates(content, "<link [^>]+\\/>");
            content = this.removeDuplicates(content, "<script [^>]+><\\/script>");
    
            response.getWriter().write(content);
        }
    
        private String removeDuplicates(String content, String regex) {
            int     index   = 0;
            Pattern pattern = Pattern.compile(regex, Pattern.DOTALL | Pattern.MULTILINE);
            Matcher matcher = pattern.matcher(content);
            while (matcher.find(index)) {
                index = matcher.end();
                content = content.substring(0, index) 
                        + content.substring(index).replace(matcher.group(), "");
                matcher = pattern.matcher(content);
            }
            return content;
        }
    }
    

    这会为每个请求增加(非常)很少的开销并暂时解决该问题。我们希望将来完全摆脱 RichFaces。

    PS:包装通过https://stackoverflow.com/a/23381235

    【讨论】:

      猜你喜欢
      • 2021-02-22
      • 2019-01-11
      • 1970-01-01
      • 1970-01-01
      • 2023-03-07
      • 1970-01-01
      • 1970-01-01
      • 2021-11-18
      • 1970-01-01
      相关资源
      最近更新 更多