2015-06-22 21 views
0

的每次點擊JSF-Primefaces PICKLIST更新我想實現以下用例 -上的標籤

<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> 

形式選項卡 - 提出了一些值,並在DB仍然存在。當tab-B被點擊時,最近持久化的值應該顯示在tab-B的PickList中。 JSF構造視圖樹並在服務器端獲得緩存,這導致Tab-B的PickList沒有更新。 尋求有經驗的JSF-Primefaces開發人員的幫助,因爲我對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> 
+1

你爲什麼不明確更新另一個選項卡中的特定組件? – Kukeltje

回答

0

嘗試TabView的緩存屬性設置爲false。從primefaces文檔

<p:tabView id="top-level-tab" cache="false"> 

引用:

當選項卡的內容懶惰通過AJAX toggleMode加載,高速緩存只 一次檢索的標籤內容和緩存標籤 的隨後切換不與服務器通信。如果關閉緩存,則每次點擊標籤時,標籤 的內容將從服務器重新加載。

0

正如已經Kukeltje的意見建議,增加在形式A(在數據庫仍然存在一個)update="..."屬性<p:commandButton ...>應該做的伎倆。

我檢查下面的代碼和它的作品:

<?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> 

支持bean NR 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; } 
} 

支持bean NR 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; } 
}