如果有人可以在这里给我帮助,我将不胜感激。
我在页面上有一个选项卡式布局,通过单击选项卡(p:commandLink),我想初始化该选项卡的适当数据并更新显示内容的区域。由于我希望初始化能够延迟发生(当呈现选项卡内容时),因此我使用 Primefaces 的 p:remoteCommand。
问题是,当我将 p:remoteCommand 设置为异步工作(async=true)时,此功能不起作用,不会调用操作方法。当属性“async”为 false 时,它会起作用。
Example:
<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:p="http://primefaces.org/ui">
<h:head>
</h:head>
<f:view contentType="text/html">
<h:form id="peopleForm">
<h:panelGroup id="panel1">
#{testbean.message}
</h:panelGroup>
<p:remoteCommand name="lazyInit"
onstart="console.log('calling init')"
action="#{testbean.init()}"
update=":peopleForm:panel1"
async="true"
/>
<script>
$(function(){
console.log('before call');
lazyInit();
console.log('after call');
});
</script>
</h:form>
<p:commandLink update=":peopleForm" value="Tab1" action="#{testbean.setMenuSelected('Tab1')}"/>
<p:commandLink update=":peopleForm" value="Tab2" action="#{testbean.setMenuSelected('Tab2')}"/>
</f:view>
</html>
@ManagedBean(name = "testbean")
@Component("testbean")
@Scope("session")
public class TestBean implements Serializable {
private static final long serialVersionUID = -2760060999550263904L;
private String message;
private String menuSelected = "Tab1";
public void init() {
if (menuSelected.equals("Tab1")) {
message = "Tab1";
}
if (menuSelected.equals("Tab2")) {
message = "Tab2";
}
}
public String getMessage() {
return message;
}
public String getMenuSelected() {
return menuSelected;
}
public void setMenuSelected(String menuSelected) {
this.menuSelected = menuSelected;
}
}
当 async="true" 时,它不起作用,点击链接时不会调用 testbean.init() 方法。当“async”为 false 时,它会起作用。
我不确定“异步”是否适用于这种类型的用例,或者我误解了它。
背景:
在我的应用程序中,我实际上有多个区域想要在选项卡更改时更新。每个区域都有自己的初始化方法,可以从数据库中提取适当的数据。我想要异步调用这些初始化方法的原因是我不希望其他初始化方法等待第一个初始化方法完成,然后等待第二个初始化方法完成等。所有这些方法都是相互独立的,因此没有它们之间同步的原因。最终,这应该会加快向用户显示页面内容的速度。
感谢您的任何帮助!
我遇到了完全相同的问题并通过替换解决了这个问题p:remoteCommand
带有隐藏的p:commandLink
。然后就没有了name
属性,因此您需要使用 JavaScript 来单击链接。
(...)
<h:head>
<script>
function lazyInitClick() {
$("[id$='lazyInit']").click();
}
</script>
</h:head>
(...)
<h:form>
<p:commandLink id="lazyInit" actionListener="#{testbean.init()}" async="true" style="display: none;" />
<h:outputScript>lazyInitClick()</h:outputScript>
</h:form>
(...)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)