我想实现以下用例-
<p:tabView id="top-level-tab">
<p:tab title="TabA" id="tab-A">
<ui:include src="tabA.xhtml" />
</p:tab>
<p:tab title="TabB" id="tab-B">
<ui:include src="tabB.xhtml" />
</p:tab>
</p:tabView>
表A中的表单提交了一些值并在DB中持久存在。单击选项卡B时,最近持久化的值应显示在选项卡B的PickList中。JSF构建视图树并在服务器端缓存,这导致tab-B的PickList没有更新。寻求经验丰富的JSF Primeffaces开发人员的帮助,因为我对JSF Primefaces非常陌生。
tabB.xhtml
<h:form id="tabBForm">
<p:pickList id="tabBPickList" value="#{tabBController.countries}" var="countries" itemLabel="#{countries}" itemValue="#{countries}" required="true"/>
<p:commandButton value="Submit" update="tabBForm"/>
</h:form>
尝试将tabView的缓存属性设置为false。
<p:tabView id="top-level-tab" cache="false">
primefaces文档中的报价:
当选项卡内容被ajax toggleMode延迟加载时,仅缓存检索一次选项卡内容以及缓存选项卡的后续切换不与服务器通信。如果缓存已关闭,则选项卡每次单击选项卡时,都会从服务器重新加载内容。
正如Kukeltje在评论中已经建议的那样,以形式A(在DB中持久存在的属性)向<p:commandButton ...>
添加update="..."
属性应该可以做到这一点。
我在下面的代码中检查了它,它有效:
<?xml version='1.0' encoding='UTF-8' ?>
<!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://xmlns.jcp.org/jsf/html"
xmlns:p="http://primefaces.org/ui">
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
<p:tabView id="tabView">
<p:tab title="TabA" id="tab-A">
<h:form id="tabAForm">
<p:outputLabel for="country" value="Enter country name" />
<p:inputText id="country" value="#{tabAController.countryName}" />
<p:commandButton value="Save" action="#{tabAController.saveCountry}" update="tabView:tabBForm:tabBPickList" />
</h:form>
</p:tab>
<p:tab title="TabB" id="tab-B">
<h:form id="tabBForm">
<p:pickList id="tabBPickList" value="#{tabBController.countries}" var="countries" itemLabel="#{countries}" itemValue="#{countries}" />
<p:commandButton value="Submit" />
</h:form>
</p:tab>
</p:tabView>
</h:body>
</html>
1号背衬豆:
import javax.enterprise.context.RequestScoped;
import javax.inject.Named;
@Named (value = "tabAController")
@RequestScoped
public class TabAController {
private String countryName;
public String saveCountry() {
CountryDAO dao = new CountryDAO();
Country country = new Country();
country.setCountryName(countryName);
dao.saveCountry(country);
return "";
}
public void setCountryName(String countryName) { this.countryName = countryName; }
public String getCountryName() { return countryName; }
}
背衬豆编号2:
import java.util.ArrayList;
import java.util.List;
import javax.enterprise.context.RequestScoped;
import javax.inject.Named;
import org.primefaces.model.DualListModel;
@Named
@RequestScoped
public class TabBController {
private DualListModel<String> countries;
public TabBController() {
CountryDAO dao = new CountryDAO();
List countriesObj = dao.getCountries();
List<String> countriesSource = new ArrayList();
for(Object country : countriesObj) {
Country tmp = (Country) country;
countriesSource.add(tmp.getCountryName());
}
List<String> countriesTarget = new ArrayList();
countries = new DualListModel(countriesSource, countriesTarget);
}
public void setCountries(DualListModel<String> countries) { this.countries = countries; }
public DualListModel<String> getCountries() { return countries; }
}