且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将动态创建的HtmlInputText组件的值绑定到bean属性?

更新时间:2022-04-30 18:22:06

HtmlInputText input1 = (HtmlInputText)application.createComponent(HtmlInputText.COMPONENT_TYPE);
input1.setId("input1"); 
//How can i set inputtext value to something 
//like: #{cRReviewReportBean.input1} in order to store 
//form data (cRReviewerFromData)?

使用 UIComponent #setValueExpression() 组件的属性。

因此,鉴于此属性是 String

private String input1;

并且有这个助手方法

public static ValueExpression createValueExpression(String valueExpression, Class<?> valueType) {
    FacesContext context = FacesContext.getCurrentInstance();
    return context.getApplication().getExpressionFactory()
        .createValueExpression(context.getELContext(), valueExpression, valueType);
}

这应该做:

HtmlInputText input1 = (HtmlInputText)application.createComponent(HtmlInputText.COMPONENT_TYPE);
input1.setId("input1"); 
input1.setValueExpression(createValueExpression("#{cRReviewReportBean.input1}", String.class));