嗯,我有一个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:My whole page
<!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>
根据注释,您只想在加载窗口后调用javascript函数。只需在<ui:define>
中定义window.onload
函数:
<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>