2015-06-22 50 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; } 
}