好吧,我有一个包含很多功能的JS文件。在特定情况下,我无法调用我的方法。参见:
<h:form>
<h:commandButton value="Montar" actionListener="#{orcamentoMB.montar}" />
</h:form>
我的actionListener是:
public void montar(){
addInfoMessage("Chamando montagem de odontograma");
RequestContext.getCurrentInstance().execute("montarOdontograma()");
}
我的Javascript功能:
function montarOdontograma() {
alert('cool, this works');
}
一个非常简单的例子,但没有用。
编辑1:我的整个页面
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui"
xmlns:pe="http://primefaces.org/ui/extensions">
<h:head>
</h:head>
<h:body>
<ui:composition template="/templates/template.xhtml">
<ui:define name="content">
<h:form>
<p:commandButton value="Montar" actionListener="#{orcamentoMB.montar}" />
<div id="container">
<div id="supe"></div>
<div id="supd"></div>
<div id="mei1e"></div>
<div id="mei1d"></div>
<div id="mei2e"></div>
<div id="mei2d"></div>
<div id="infe"></div>
<div id="infd"></div>
</div>
</h:form>
</ui:define>
</ui:composition>
</h:body>
</html>
编辑2:尝试使用onload来调用javascript
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui"
xmlns:pe="http://primefaces.org/ui/extensions">
<h:head>
</h:head>
<h:body onload="montarOdontograma()">
<ui:composition template="/templates/template.xhtml">
<ui:define name="content">
<h:form id="formManterOrcamento">
<p:remoteCommand process="@this"
actionListener="#{orcamentoMB2.montarOdontograma}"
name="montarOdontograma" />
解决:
RequestContext.getCurrentInstance()。execute()只是处理ajax请求。在我的情况下,我只是将RequestContext更改为直接使用标记调用,请参阅下面的内容:
<script>
function init(){
montarOdontograma();
}
window.onload = init;
</script>
答案 0 :(得分:0)
基于注释,您只想在窗口加载后调用javascript函数。只需在window.onload
:
<ui:define>
功能即可
<h:body>
<ui:composition template="/templates/template.xhtml">
<ui:define name="content">
<script type="text/javascript">
window.onload = function() {
montarOdontograma();
//more JavaScript code you want/need to execute...
};
</script>
<h:form>
<!-- rest of your JSF code... -->
</h:body>