Javascript不会从ManagedBean中的RequestContext调用
Javascript is not called from RequestContext in ManagedBean
嗯,我有一个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>
相关文章:
- 无法从 jQuery RSS Feed 中的 localStorage 动态替换类
- 可以't让我的if语句处理js中的html表单输入
- 引用对象中的通用值
- 使用JS如何动态更改显示的html文件中的文本背景颜色
- 用程序搜索JQuery数据表中的文本
- 无法导出函数expressjs/requestjs中的变量
- 函数参数中的数据与指定变量之间的任何性能差异
- 不能从angular2中的子组件指定父组件中的数组
- Dojo不解析自定义小部件的模板html中的小部件声明性
- 如何在angularJS中编辑时,如果DB中的值为true,则设置复选框,如果值为false,则取消选中复选框
- 正在验证8个真/假复选框或复选框中的2个
- Javascript-如何读取json文件中的列并将其保存在Javascript数组中
- 如何更改bigquery API中的计费层选项
- 将输入字段中的文本提交到我的数据库,同时将其添加到我的列表中
- 将jsp文件下拉列表中的选定项分配给一个java变量(比如String selection)
- 使用Dnamics CRM 2011中的JavaScript读取子网格的所有记录,而不考虑活动页面
- 拨打'父亲'函数形式a'儿童'ReactJS中的组件
- 我如何找到一个句子中的所有空格并替换忽略它们
- zurb基础中的固定宽度立柱3
- Javascript,访问一个主要对象模块模式中的每个对象