【发布时间】:2013-02-27 00:41:28
【问题描述】:
我有下一个问题!
我想从我的支持 bean 动态创建 inputtext,它们将在一个选项卡内,也是动态创建的,将在执行时构建。
我设法使用相应的input 类动态添加组件。
但我还没有设法将值标签添加到组件中,这是一种 valueExpresion 语言,它将值绑定到 managedBean 本身。
我找到了一些可以像这样总结的代码。
@ManagedBean
@ViewScoped
public MyManagedBean(){
private TabView tabsi;
HtmlOutputLabel hol = new HtmlOutputLabel();
InputText txt2 = new InputText();
private String value;
/* getter and setters */
public void MyManagedBean{
tabsi = new TabView();
Tab tab1 = new Tab();
tab1.setTitle("Tab1");
Tab tab2 = new Tab();
tab2.setTitle("Tab2");
tabsi.getChildren().add(tab1);
tabsi.getChildren().add(tab2);
hol.setValue("label");
hol.setStyleClass("label");
txt2.setValueExpression("value",
TestController.getExpression("#{myManagedBean.value}"));
txt2.setValue(value);
tab1.getChildren().add(hol);
tab1.getChildren().add(txt2);
}
public static ValueExpression getExpression(String expression) {
FacesContext fc = FacesContext.getCurrentInstance();
ELContext ctx = fc.getELContext();
ExpressionFactory factory = fc.getApplication().getExpressionFactory();
return factory.createValueExpression(ctx, expression, Object.class);
}
public void test1() {
System.out.println(value);
}
}
我成功地构建了组件,但我无法将它绑定到设置ValueExpression。当我从按钮调用 test1 函数时,它会打印 null
如何将值绑定到ManagedBean???
【问题讨论】:
-
你调用 TestController.getExpression("#{myManagedBean.value}"));在 MyManagedBean 的构造函数中。你不能从正在建设的东西中得到任何东西。它总是返回 null。
-
在构造函数中,我建立了绑定的值!所以在我触发 actionListener 之前它不会被调用,在我的例子中是我按下的按钮。
标签: jsf