Primefaces javascript延迟解析

Primefaces javascript延迟解析,javascript,jsf,jsf-2,primefaces,primefaces-extensions,Javascript,Jsf,Jsf 2,Primefaces,Primefaces Extensions,从PageSpeed Insights可以看出,Primefaces 4.0在页面加载过程中产生了大量开销: **605.3KiB of JavaScript is parsed during initial page load. Defer parsing JavaScript to reduce blocking of page rendering.** http://localhost:8888/.../primefaces.js.xhtml?... (219.5KiB) http://l

从PageSpeed Insights可以看出,Primefaces 4.0在页面加载过程中产生了大量开销:

**605.3KiB of JavaScript is parsed during initial page load. Defer parsing JavaScript to reduce blocking of page rendering.**
http://localhost:8888/.../primefaces.js.xhtml?... (219.5KiB)
http://localhost:8888/.../jquery-plugins.js.xhtml?... (191.8KiB)
http://localhost:8888/.../jquery.js.xhtml?... (95.3KiB)
http://localhost:8888/.../tooltip.js.xhtml?... (34.5KiB)
http://localhost:8888/.../jsf.js.xhtml?... (25.4KiB)
http://localhost:8888/.../primefaces-extensions.js.xhtml?... (19.7KiB)
http://localhost:8888/.../watermark.js.xhtml?... (4.7KiB)
http://localhost:8888/.../hotkey.js.xhtml?... (1.2KiB)

您知道如何将这些第三方javascript文件设置为位于正文部分的底部,而不是头部或使用延迟/异步参数吗?Javascript加载程序在这种情况下没有帮助,因为它们来自JSF渲染器。我还尝试为PreRenderView()创建一个侦听器,但没有成功。还有其他解决这个问题的方法吗?谢谢你的帮助

我将脚本移动到以下代码段:

public class ScriptValidateListener implements SystemEventListener {

    @Override
    public void processEvent(SystemEvent event) throws AbortProcessingException {
        UIViewRoot root = (UIViewRoot) event.getSource();
        FacesContext ctx = FacesContext.getCurrentInstance();
        List<UIComponent> resources = root.getComponentResources(ctx, "HEAD");
        for (UIComponent r : resources) {
            String name = (String) r.getAttributes().get("name");
            if (name == null) {
                continue;
            }

            if (name.contains(".js")) {
                root.removeComponentResource(ctx, r, "HEAD");
                root.addComponentResource(ctx, r, "BODY");
            }
        }
    }

    @Override
    public boolean isListenerForSource(Object source) {
        return (source instanceof UIViewRoot);
    }
}
公共类ScriptValidateListener实现SystemEventListener{
@凌驾
public void processEvent(SystemEvent事件)引发AbortProcessingException{
UIViewRoot根=(UIViewRoot)事件。getSource();
FacesContext ctx=FacesContext.getCurrentInstance();
列表资源=root.getComponentResources(ctx,“HEAD”);
用于(UIR组件:资源){
字符串名称=(字符串)r.getAttributes().get(“名称”);
if(name==null){
继续;
}
if(name.contains(“.js”)){
root.removeComponentResource(ctx,r,“头部”);
addComponentResource(ctx,r,“BODY”);
}
}
}
@凌驾
公共布尔isListenerForSource(对象源){
返回(UIViewRoot的源实例);
}
}
这会将所有Java脚本从主体的头部移动到末端。但是Primefaces有一个问题,即呈现的组件将尝试访问JQuery($)或Primefaces javascript函数,这将破坏页面上的所有ajax功能。也许我需要决定哪些脚本需要移动,哪些脚本不需要移动。此外,我还需要从侦听器中为faces-config.xml定义以下内容以使其正常工作:

<application>
    <system-event-listener>
        <system-event-listener-class>com.example.listener.ScriptValidateListener</system-event-listener-class>
        <system-event-class>javax.faces.event.PreRenderViewEvent</system-event-class>
    </system-event-listener>
</application>

com.example.listener.ScriptValidateListener
javax.faces.event.PreRenderViewEvent

我将脚本移动到以下代码段:

public class ScriptValidateListener implements SystemEventListener {

    @Override
    public void processEvent(SystemEvent event) throws AbortProcessingException {
        UIViewRoot root = (UIViewRoot) event.getSource();
        FacesContext ctx = FacesContext.getCurrentInstance();
        List<UIComponent> resources = root.getComponentResources(ctx, "HEAD");
        for (UIComponent r : resources) {
            String name = (String) r.getAttributes().get("name");
            if (name == null) {
                continue;
            }

            if (name.contains(".js")) {
                root.removeComponentResource(ctx, r, "HEAD");
                root.addComponentResource(ctx, r, "BODY");
            }
        }
    }

    @Override
    public boolean isListenerForSource(Object source) {
        return (source instanceof UIViewRoot);
    }
}
公共类ScriptValidateListener实现SystemEventListener{
@凌驾
public void processEvent(SystemEvent事件)引发AbortProcessingException{
UIViewRoot根=(UIViewRoot)事件。getSource();
FacesContext ctx=FacesContext.getCurrentInstance();
列表资源=root.getComponentResources(ctx,“HEAD”);
用于(UIR组件:资源){
字符串名称=(字符串)r.getAttributes().get(“名称”);
if(name==null){
继续;
}
if(name.contains(“.js”)){
root.removeComponentResource(ctx,r,“头部”);
addComponentResource(ctx,r,“BODY”);
}
}
}
@凌驾
公共布尔isListenerForSource(对象源){
返回(UIViewRoot的源实例);
}
}
这会将所有Java脚本从主体的头部移动到末端。但是Primefaces有一个问题,即呈现的组件将尝试访问JQuery($)或Primefaces javascript函数,这将破坏页面上的所有ajax功能。也许我需要决定哪些脚本需要移动,哪些脚本不需要移动。此外,我还需要从侦听器中为faces-config.xml定义以下内容以使其正常工作:

<application>
    <system-event-listener>
        <system-event-listener-class>com.example.listener.ScriptValidateListener</system-event-listener-class>
        <system-event-class>javax.faces.event.PreRenderViewEvent</system-event-class>
    </system-event-listener>
</application>

com.example.listener.ScriptValidateListener
javax.faces.event.PreRenderViewEvent