2014-11-08 51 views
0

我正在JSF 2.1和PrimeFaces 5.0上工作,我正嘗試從@PostConstructor創建TabView,但無法實現。有可能使此標籤視圖變爲可能。如何通過@PostConstructor創建tabView

@PostConstructor但無法使其正確發生。

XHTML來源:

<div align="center"> 
    <h1 style="margin-top:0; color: cornflowerblue">Basic User Form</h1> 
</div> 

<h:form id="initTest" > 
    <p:tabView binding="#{initTestMgBean.tabView}"/> 
</h:form> 

ManagedBean來源:

@ManagedBean 
@RequestScoped 
public class initTestMgBean { 

    private TabView tabView; 

    public TabView getTabView() { 
     return tabView; 
    } 

    public void setTabView(TabView tabView) { 
     this.tabView = tabView; 
    } 

    @PostConstruct 
    public void init() { 
     tabView = new TabView(); 
     tabView.setInView(true); 

     Tab tab1 = new Tab(); 
     tab1.setTitle("User Form 1"); 


     Tab tab2 = new Tab(); 
     tab2.setTitle("User Form 2"); 

     tabView.getChildren().add(tab1); 
     tabView.getChildren().add(tab2); 

     PanelGrid panel1 = new PanelGrid(); 
     panel1.setId("panel1"); 
     panel1.setColumns(2); 

     PanelGrid panel2 = new PanelGrid(); 
     panel2.setId("panel2"); 
     panel2.setColumns(2); 

     HtmlOutputLabel label = new HtmlOutputLabel(); 
     label.setId("outlab"); 
     label.setValue("Name "); 
     label.setFor("name"); 

     InputText text = new InputText(); 
     text.setId("name"); 
     text.setSize(15); 
     text.setStyle("height:30px"); 

     HtmlOutputLabel label4 = new HtmlOutputLabel(); 
     label4.setId("outlab4"); 
     label4.setFor("userName"); 
     label4.setValue("User Name"); 

     InputText text4 = new InputText(); 
     text4.setId("userName"); 
     text4.setSize(15); 
     text4.setStyle("height:30px"); 

     tab1.getChildren().add(panel1); 

     panel1.getChildren().add(label); 
     panel1.getChildren().add(text); 

     tab2.getChildren().add(panel2); 
     panel2.getChildren().add(label4); 
     panel2.getChildren().add(text4); 
    }  
} 

回答

0

你試過注射它來代替?試試這個,看看它是否工作:

@Named 
@RequestScoped 
public class TabViewBean implements Serializable { 

    private TabView tabView; 

    public TabViewBean(){ } 

    @Inject 
    public TabViewBean(SomeOtherBean unused){ 

     tabView = new TabView(); 

     Tab tab = new Tab(); 
     tab.setTitle("I'm a title");    
     tabView.getChildren().add(tab); 

     //Tab tab2 = new Tab(); 
     //tab2.setTitle(unused.getTitle()); 
     //tabView.getChildren().add(tab2); 

    } 

    public TabView getTabView(){ 
     return tabView; 
    } 

    public void setTabView(TabView tabView){ 
     this.tabView = tabView; 
    } 

} 

我已經包含另一個bean只是作爲一個例子,如何注入時訪問其他豆類。你不必使用它。

+0

無法實現其添加TabView的動態,你可以給有關注射或如何我實現它..感謝名單答覆一些想法 – Kandy 2014-11-10 09:23:36

+0

從Oracle閱讀:http://www.oracle.com/technetwork/articles/java/cdi-javaee-bien-225152.html或更具體的教程在這裏:http://www.javacodegeeks.com/2013/ 05/java -ee-cdi-dependency -injection-inject-tutorial.html – 2014-11-10 16:10:31

+0

感謝@Mattias F提供良好的學習鏈接。 – Kandy 2014-11-11 04:58:01

0

通過這種方式,我們能夠通過的Xhtml

@ManagedBean 
 
@SessionScoped 
 

 
public class TabViewBean implements Serializable { 
 
    
 
    public TabViewBean() { 
 
      fc = FacesContext.getCurrentInstance(); 
 
      tabView = (TabView) fc.getApplication().createComponent(
 
        "org.primefaces.component.TabView"); 
 
      Tab tab1 = new Tab(); 
 
      tab1.setTitle("User Form 1"); 
 
      Tab tab2 = new Tab(); 
 
      tab2.setTitle("User Form 2"); 
 
      
 
      tabView.getChildren().add(tab1); 
 
      tabView.getChildren().add(tab2); 
 

 
      tabView.setActiveIndex(0); 
 
     } 
 

 
    public TabView getTabView() { 
 
     return tabView; 
 
    } 
 

 
    public void setTabView(TabView tabView) { 
 
     this.tabView = tabView; 
 
    } 
 

 
    public void addTabs() { 
 
     Tab tab3 = new Tab(); 
 
     tab3.setTitle("Manage Favorites"); 
 
     tab3.setClosable(true); 
 
     tabView.getChildren().add(tab3); 
 
     System.out.println("Called"); 
 
    } 
 

 
    FacesContext fc; 
 
    TabView tabView; 
 

 
}