Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

InputText in DataTable does not store value

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Guys,

I have been bumping my head against the wall on this issue. I appreciate any insights...

I have a InputText within a DataTable. When user clicks on "Add New Row" commandButton, it calls ActionListener which calls a method in BackingBean to add a new empty row to the table. Once the new row is created on the web page, the user enters the data in the row and clicks on "Save" which triggers an action call to the backing bean, which for some reason does not retrieve the user entered data. Please see below on code and appreciate any help I can get to resolve this issue.

Thanks in advance. Looking forward to your response anxiously.

Facelets -> content.xhtml
<h:panelGroup>
<t:dataTable id="foodTable" border="1" styleClass="tableStyle"
value="#{foodBean.foodItems}" var="food"
headerClass="tableHeader" rowClasses="tableRow">
<t:column>
<f:facet name="header" id="h1">
<t:outputText value="Item" />
</f:facet>
<t:inputText id="itemId" size="10" styleClass="inputTextClass"
value="#{food.item}" />
</t:column>
<t:column>
<f:facet name="header" id="h2">
<t:outputText value="Calories" />
</f:facet>
<t:inputText id="caloriesId" size="4" value="#{food.calories}" />
</t:column>
<t:column>
<f:facet name="header" id="h3">
<t:outputText value="Fat(Calories)" />
</f:facet>
<t:inputText id="fatCaloriesId" size="4"
value="#{food.fatCalories}" />
</t:column>
<t:column>
<f:facet name="header" id="h4">
<t:outputText value="Portion" />
</f:facet>
<t:inputText id="portionId" size="2" value="#{food.portion}" />
</t:column>
</t:dataTable>
<h:commandButton value="Add Food Items" type="submit"
actionListener="#{foodBean.addItem}"/>
<h:outputText value=" " />
<h:commandButton value="Save Food Items" type="submit"
action="#{foodBean.executeFoodData}" />
<h:outputText value=" " />
</h:panelGroup>


Backing Bean action method called by "Save" commandButton:
public String executeFoodData() {
log.debug("executeFoodData() with foodItems size of "
+ foodItems.size());

Iterator<Food> f = foodItems.iterator();

while (f.hasNext()) {
Food fd = f.next();
log.debug("food Items is " + fd.getItem() + " With Calories "
+ fd.getCalories());
}
foodService.storeFoodDateAll(foodItems);
return Constants.SUCCESS;
}
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try binding your datatable using the binding attribute.
Tie the binding to a variable in a backing bean .



that way you should get a handle to your newly added row. Maybe this might help http://balusc.xs4all.nl/srv/dev-jep-dat.html.


cheers, sachin
 
Pinal N Patel
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Sachin. I added binding attribute to the table and from the action method, I retrieve HTMLDataTable (using getter method corresponding to binding attribute) and get the value of the tables using getValue(). This works and all is well.

However, shouldn't value attribute in the <h:datatable> update getter/setter . Why set binding attribute and making the code little complex? Or, without binding the table, I could retrieve the Table value using facesContext, but this makes code complex too?

I thought JSF dynamically populates table values in the ArrayList(value is set to Arraylist object).

Regards,
Pinal
 
Pinal N Patel
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One more question, I have seen many examples on the internet regarding the user of HTML Data Table with inputText and I assume they all work correctly. But for some reason, the above code does not populate instance variables in the backing bean during UPDATE MODEL VALUES Phase in JSF lifecycle. Please look at the code above and share any thoughts on resolving this issue.

I thought as long as you provide getter/setter in backing bean class, if a component is modified, then those values will be updated to the backing bean class in UPDATE MODEL VALUES. Is this not correct?

Regards,
Pinal
 
Ranch Hand
Posts: 536
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
whats the scope of your backing bean? request or session?
 
Pinal N Patel
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've tried both and still no success :-)

Regards,
Pinal
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So I am curious if you have made progress since your last post here? I am having a similar issue. I render a <t:dataTable> in the page, it is populated from a collection of objects (e.g. Foo.java). One of the fields on Foo needs to be updated (e.g. Foo.updateMe is a boolean that renders itself as a radio within the <t:dataTable>). So:


In the backing bean:


When I hit the <save> button, the List<Foo> I fetch from updatedFoos.getValue() does not contain any updates (e.g. - The radios were all toggled however the member property from the collection does not reflect this).

Am I going down the wrong path? Any help would be appreciated.

Thanks -
cporter
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try using preserveDataModel="true" on dataTable component. By default this attribute is false and data model is refreshed everytime page is rendered.

Thanks
Ravindra
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have exactly the same problem as C Porter.
The update I made in the inputtext is not represented in the List.

Another question I have:
What is the problem, when I save with the button, that my object model is not filled (by using setters) ?
In Debug mode I can see that a new instance is using my setters.
Therefore the different instances causes the problem, that I have to use the binding.
Any explaination for me?
Thanks,
Klaus
 
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please start your own topic for each independent problem. Do not revive old topics for own problems where in others are more likely to respond on an aged question of the topicstarter and thus completely overlook or even ignore you.
 
Klaus Schuster
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i have exactly the same problem. i don't understand why I have to copy the same code with the same question ?!?
I started a new thread as wished: Same Problem, same forum, new thread
 
We can fix it! We just need some baling wire, some WD-40, a bit of duct tape and this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic