How can I conditionally render .js file in h:head,

2019-08-07 07:16发布

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条回答
Evening l夕情丶
2楼-- · 2019-08-07 07:56

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.

查看更多
登录 后发表回答