Ajax 在 JSF 中不起作用



我不知道为什么Ajax在我的JSF文件中不起作用。甚至我有一个最新的Mojarra,它是3.2.13在我的GlassFish。这是我的代码

<h:head></h:head>
<h:body>
<h:panelGroup id="panel" rendered="#{chartBean.showCharts}">
<p:barChart id="basic" value="#{chartBean.categoryModel}"
legendPosition="ne" title="Basic Bar Chart" min="0" max="200"
style="height:300px" />
</h:panelGroup>
<h:commandButton value="Display Chart" action="#{chartBean.createCategoryModel}">
<f:ajax render="panel"/>
</h:commandButton>
</h:body>

现在,当我运行此代码时,按钮不起作用。

另一件事,如果我像删除面板一样更改我的代码,然后像 <f:ajax render="basic"/>一样在条形图上渲染并将其放置在<form>然后它抛出NullPointerException.因为它呈现图表并期望从chartBean.categoryModel的值。现在 AFAIK 它不应该因为 ajax 而被渲染。

我无法理解 JSF 中的 ajax 行为。我也尝试了<f:ajax execute="@form" render="basic">但它也没有用。这是如何造成的,我该如何解决?

不能重新呈现在单击 ajax 按钮时页面上不存在的组件。

一个简单的解决方案是将渲染的属性从h:panelGroup移动到p:barChart。然后,将始终呈现 panelGroup,JSF 会在您的 ajax 请求期间找到它。

<h:panelGroup id="panel">
<p:barChart rendered="#{chartBean.showCharts}"
id="basic" value="#{chartBean.categoryModel}"
legendPosition="ne" title="Basic Bar Chart" min="0" max="200"
style="height:300px" />
</h:panelGroup>
<h:commandButton value="Display Chart" action="#{chartBean.createCategoryModel}">
<f:ajax render="panel"/>
</h:commandButton>

最新更新