如何在facelet页面中包含外部javascript文件?

时间:2012-05-17 05:20:32

标签: jsf-2 primefaces

在使用Facelet模板的Facelet页面中:

<h:head>
    <h:outputScript name="custom.js" library="javascript" target="head"/>
</h:head>
<ui:composition template="../../WEB-INF/Template.xhtml">

</ui:composition>

如何添加自定义JavaScript?在上面的代码中,我的custom.js被忽略,因为 ui:composition facelet标签。

我不想通过将我的javascript放入其中来弄乱我的页面,所以我在我的资源中将其外化 文件夹中。

但我如何实现目标?

更新

我基本上有这个按钮,我想在我的primefaces按钮的oncomplete事件上添加自定义javascript。

<p:commandButton value="Save"
        actionListener="#{memberManagedBean.save}"
        oncomplete="handleSaveNewMember(xhr, status, args)"
        update=":memberListForm:membersTable"
        process="@form" />

但是我没有把我的代码放到它上面,而是把它外化到我的custom.js

function handleSaveNewMember(xhr, status, args) {
    /*More Code*/
    addMemberDlg.hide();
}

但是查看为我的按钮生成的HTML,我的自定义javascript代码不包括在内,只添加了函数名称。

<button id="createupdateform:j_idt18" oncomplete:function(xhr, status, args){handleSaveNewMember(xhr, status, args);}});return false;" type="submit"><span class="ui-button-text">Save</span></button>

为什么你认为是这样?

更新2

您应该将脚本插入ui:define facelet标记内,而不是在ui:composition。

<ui:composition template="../../WEB-INF/Template.xhtml">
    <ui:define name="content">
        <h:outputScript name="showmembers.js" library="javascript" target="head"/>
    </ui:define>
</ui:composition>

1 个答案:

答案 0 :(得分:3)

您可以将其放在<ui:composition

<ui:composition template="../../WEB-INF/Template.xhtml">
    <h:outputScript name="custom.js" library="javascript" target="head"/>
</ui:composition>

或将<h:outputScript放在Template.xhtml

无论如何,<h:head>最好只放在Template.xhtml ...

相关问题