1

I have some fields of a bean that I need to show programmatically. Is something like this:

         HtmlPanelGrid panel = new HtmlPanelGrid();
         panel.setColumns(4);

         HtmlOutputLabel fieldOut = new HtmlOutputLabel();
         fieldOut.setId("fieldOutId");
         fieldOut.setValue("name");
         panel.getChildren().add(fieldOut);
         HtmlInputText fieldIn = new HtmlInputText();
         fieldIn.setId("fieldInId");
         fieldIn.setPartialSubmit(true);
         fieldIn.setValueExpression(
          "field", UtilFaces.createValueExpression("#{newElementBean.fieldName}",String.class));

         panel.getChildren().add(fieldIn); 
         mainForm.getChildren().add(panel);

In newElements.xhtml i've defined a form which is binded to mainForm, in this way:

    <ice:form binding="#{newElementBean.mainForm}">
  <ice:inputText id="ANOTHERFIELD" value="#{newElementBean.anotherField}"/>
  <ice:commandButton  action="#{newElementBean.save}"  value="Save"/>
</ice:form>

When I click on the save button and I go to the next view, the field "ANOTHERFIELD" has taken the value from the bean, and shows up correctly, but the fields that were dinamically generated shows empty. Its values in the backing bean also are null. It's like the ValueExpression is not working for the HtmlInputText that I created in the backing bean. I'm using Icefaces 3.3 with Mojarra 2.1.17.

How is this caused and how can I solve it?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Jose__vi
  • 158
  • 2
  • 10
  • when are you creating the fields? – Philipp Sander Sep 03 '13 at 07:50
  • In a previous bean, i get the bean with newElementBean = (NewElementBean)UtilFaces.getBean("newElementBean"), then i create the fields and set to the bean with newElementBean.setMainForm(form). – Jose__vi Sep 03 '13 at 07:56
  • The scope of these beans is @CustomScoped(value = "#{window}"). I tried with noneScope or sessionScope, just for seeing if the values were correct, but didnt work. – Jose__vi Sep 03 '13 at 08:15
  • I have news. The setValueExpression is wrong, the first parameter must be "value" (fieldIn.setValueExpression("value", UtilFaces.createValueExpression("#newElementBean.fieldName}",String.class))). Is not resolved yet, right now the field is showing the text "#{newElementBean.fieldName}". – Jose__vi Sep 03 '13 at 09:15
  • SOLVED!! I cannot post the answer because of my reputation :-(. In a few words,the first parameter of the setValueExpression must be "value". My second mistake was that my createValueExpression method was incorrect. In this link, at the bottom, you will find the method that works for me. – Jose__vi Sep 03 '13 at 09:51
  • I did not put the link. Here is it. https://gist.github.com/salikes/3728993/raw/902504805767dcc69a765f52b355b51107621a47/PersistStateDataModel.java – Jose__vi Sep 03 '13 at 10:22

1 Answers1

2

I solved it. I made two mistakes:

  1. The proper setValueExpression call is like this:

    fieldIn.setValueExpression("value", UtilFaces.createValueExpression("#newElementBean.fieldName}");
    

    I was incorrectly using "field1" as 1st argument instead of "value".

  2. This is not visble in the question, but my createValueExpression() helper method was also wrong. The following is working for me:

    public static ValueExpression createValueExpression(String expression) {
        Application app = FacesContext.getCurrentInstance().getApplication();
        ExpressionFactory elFactory = app.getExpressionFactory();
        ELContext elContext = FacesContext.getCurrentInstance().getELContext();
        return elFactory.createValueExpression(elContext, expression, Object.class);
    }
    
Jose__vi
  • 158
  • 2
  • 10