How can I conditionally render .js file in h:head, if file was not rendered after Full Page Refresh (FPR)?

做~自己de王妃 提交于 2019-12-22 14:15:06

问题


PrimeFaces conditionally renders the following in h:head:

<script type="text/javascript" src="/webapp/javax.faces.resource/push/push.js.jsf?ln=primefaces"><!--//--></script>

when PrimeFaces p:socket component is added to JSF/xhtml page. Some of my page refreshes, result in this push.js file 'not' being rendered in h:head.

When push.js file is 'not' rendered, I would like to conditionally render the push.js file in attempt to fix/workaround an issue I'm having with PrimeFaces Push.

For more details, please click PrimeFaces forum topic URL below:

Uncaught TypeError: undefined is not a function

I am asking this question, because I am already conditionally rendering h:head based on bean attribute values, such as the following:

<h:head rendered="#{!pageNavigationController.gmaps and !pageNavigationController.gmapsAutoComplete}">
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <meta http-equiv="refresh" content="#{session.maxInactiveInterval};url=pf_viewExpired.jsf" />
    <title><h:outputText escape="false" value="MCMS"/></title>
    <h:outputStylesheet name="css/mcms.css"/>
    <h:outputScript name="js/mcms.js"/>
    <h:outputScript name="js/addUsingTemplate.js"
                    rendered="#{pageNavigationController.isPageSelected('/orders/pf_AddUsingTemplate.xhtml')}"/>
</h:head>

Maybe my use of h:head is making it difficult for the container TomEE 1.5 SNAPSHOT to serve the .js file, because of the conditional rendering of multiple h:head on the page, or this may actually be a PrimeFaces Push (Atmosphere) issue that ultimately needs to be solved, but I'm sure there is a way to render the .js file via h:outputScript, if the file is not rendered in h:head already.

The web application is using the following:

PrimeFaces 3.5 SNAPSHOT, TomEE 1.5 SNAPSHOT (Tomcat 7.0.32), JUEL 2.2.5, OmniFaces 1.3 SNAPSHOT

Please advise.


回答1:


This seems more like a PrimeFaces issue. In order to force loading of push.js on every request on the same view, you could explicitly add a

<h:outputScript library="primefaces" name="push/push.js" target="head" /> 

without conditional rendering. If everything went well, i.e. it behaves well as per JSF2 resource handling mechanism, then it should not be included twice at all in cases where it worked properly.



来源:https://stackoverflow.com/questions/13532172/how-can-i-conditionally-render-js-file-in-hhead-if-file-was-not-rendered-afte

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!