如何将动态创建的HtmlInputText组件的值绑定到bean属性? [英] How to bind value of dynamically created HtmlInputText component to bean property?

查看:112
本文介绍了如何将动态创建的HtmlInputText组件的值绑定到bean属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何动态设置JSF dynamicPanelGrid与某些组件(例如某些inputtexts),然后获取它们的值并将它们存储到对象和数据库中?我创建了一个示例对象模型,一个带有占位符的Facelets视图,用于动态生成的 HtmlPanelGrid 和一个支持bean,我创建 HtmlPanelGrid 实例及其组件。以下是每个代码:

How can I set dynamically a JSF dynamicPanelGrid with some components (e.g. some inputtexts) and then fetch their values and store them into the object and database? I have created a sample object model, a Facelets view with a "placeholder" for the dynamically generated HtmlPanelGrid and a backing bean where I create the HtmlPanelGrid instance and its components. Here is the code for each:

型号:

@Entity
@Table(name = "imageviewer_crreviewerformdata")
public class CRReviewerFormData implements Serializable {

private static final long serialVersionUID = 1L;

@Id
@GeneratedValue
@Column(name = "FdId")
private Long fdId;

@Column(name = "Input1")
private String input1;

@Column(name = "Input2")
private String input2;

@Column(name = "Input3")
private String input3;

/* getters & setters*/
// ...

视图:

<p:commandButton value="View" action="#{reviewReportBean.updateDynamicPanelGrid()}"
                   oncomplete="dlg.show()" icon="ui-icon-image" >
    <f:param name="selectedImage" value="#{cRImageData.imName}" />
</p:commandButton>
...
<p:outputPanel id="outerContainerDynamicPanelGrid" autoUpdate="true">
    <h:panelGrid id="innerContainerDynamicPanelGrid"
                    binding="#{reviewReportBean.dynamicPanelGrid}">
    </h:panelGrid>
</p:outputPanel>
<p:commandButton id="viewSaveForm" value="Save" 
                    action='#{reviewReportBean.saveReport()}'>
</p:commandButton>
<p:commandButton id="viewEditForm" value="Edit" 
                    action='#{reviewReportBean.editReport()}'>
</p:commandButton>
...

控制器:

@ManagedBean(name = "reviewReportBean")
@ViewScoped
public class ReviewReportBean implements Serializable { 
    private static final long serialVersionUID = 1L;
    private String imageOfInterest;
    private HtmlPanelGrid dynamicPanelGrid;
    private CRReviewerFormData cRReviewerFromData;
    // ...
    @PostConstruct
    public void init(){     
        dynamicPanelGrid = new HtmlPanelGrid();
        FacesContext facesContext = FacesContext.getCurrentInstance();
        ConfigOptionsBean configOptionsBean = (ConfigOptionsBean) facesContext.getApplication().getVariableResolver().resolveVariable(facesContext, "configOptionsBean");

        cRReviewerFromData = new CRReviewerFormData();
    }

    @SuppressWarnings("unused")
    public void updateDynamicPanelGrid() {

        RequestContext requestContext = RequestContext.getCurrentInstance();
        Application application = FacesContext.getCurrentInstance().getApplication();
        dynamicPanelGrid.getChildren().clear();

        Row row1 = (Row) application.createComponent(Row.COMPONENT_TYPE);
        row1.setRendered(true);
        Row row2 = (Row) application.createComponent(Row.COMPONENT_TYPE);
        row2.setRendered(true);     
        Row row3 = (Row) application.createComponent(Row.COMPONENT_TYPE);
        row3.setRendered(true);

        HtmlOutputLabel label1 = (HtmlOutputLabel)application.createComponent(HtmlOutputLabel.COMPONENT_TYPE);      
        label1.setValue("I am the first label");
        label1.setStyle("font-weight:bold;color:black");
        label1.setId("label1");     
        HtmlOutputLabel label2 = (HtmlOutputLabel)application.createComponent(HtmlOutputLabel.COMPONENT_TYPE);      
        label2.setValue("I am the second label");
        label2.setStyle("font-weight:bold;color:red");
        label2.setId("label2");     
        HtmlOutputLabel label3 = (HtmlOutputLabel)application.createComponent(HtmlOutputLabel.COMPONENT_TYPE);      
        label3.setValue("I am the third label");
        label3.setStyle("font-weight:bold;color:red");
        label3.setId("label3");

        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)?

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

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

        dynamicPanelGrid.setColumns(2);

        dynamicPanelGrid.getChildren().add(label1);
        dynamicPanelGrid.getChildren().add(input1);     
        dynamicPanelGrid.getChildren().add(label2);
        dynamicPanelGrid.getChildren().add(input2);     
        dynamicPanelGrid.getChildren().add(label3);
        dynamicPanelGrid.getChildren().add(input3);
        requestContext.update(":viewDatagridForm:innerContainerDynamicPanelGrid");
    }
    // ...
}

怎么能我将动态创建的 HtmlInputText 组件的值绑定到bean属性?

How can I bind the value of dynamically created HtmlInputText components to bean properties?

推荐答案

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));

这篇关于如何将动态创建的HtmlInputText组件的值绑定到bean属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆