我正在研究JSF 2.1和PrimeFaces 5.0,我正在尝试从TabView
创建@PostConstructor
但是无法实现它。可以像这样制作标签视图。
@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 :(得分: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作为示例包含在注入时如何访问其他bean。你不必使用它。
答案 1 :(得分:0)
通过这种方式,我们可以在Xhtml上动态添加tabView
@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;
}