首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何通过@PostConstructor创建tabView

如何通过@PostConstructor创建tabView
EN

Stack Overflow用户
提问于 2014-11-08 13:57:46
回答 2查看 169关注 0票数 0

我正在JSF2.1和PrimeFaces 5.0上工作,我正在尝试从@PostConstructor创建TabView,但无法让它happen.Is像这样的选项卡视图。

@PostConstructor,但不能使其正常发生。

XHTML源:

代码语言:javascript
复制
<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来源:

代码语言:javascript
复制
@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);
    }    
}
EN

回答 2

Stack Overflow用户

发布于 2014-11-08 20:35:34

你有没有试过注射它?试试这个,看看它是否有效:

代码语言:javascript
复制
@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,只是作为如何在注入时访问其他bean的示例。你不需要使用它。

票数 0
EN

Stack Overflow用户

发布于 2014-11-10 20:47:35

通过这种方式,我们能够在Xhtml上动态添加tabView

代码语言:javascript
复制
 @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;

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26814286

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档