2012-05-17 175 views
2

在为使用的facelet模板这样的一个facelet页:如何在外部页面中包含外部JavaScript文件?

<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放入其中来混淆我的页面,所以我在我的资源文件夹 中将其外化。

但是我如何实现我的目标?

UPDATE:

我基本上有这样的按钮,我想添加自定义JavaScript我primefaces按钮的onComplete事件。

<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

你应该插入脚本界面内:不定义用户界面内Facelet标记:组成。

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

回答

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只有...

+0

喜丹尼尔,感谢这一点,但你能帮我解决问题吗?我已更新我的帖子。 –

+0

我的答案是否解决了您的原始问题?关于你的新问题,你不应该看到你的handleSaveNewMember代码附加到你的按钮中,它的确定,它会在你的js文件中......在你支持的按钮中只能看到对你发布的函数名称的引用,你可能需要点击ctrl + F5一次刷新你的js – Daniel

+0

嗨丹尼尔..对不起,我不确定我的问题是否真的回答,因为我点击按钮时我的对话框不是很接近。什么都没有发生。我在想我的代码没有执行。 –