File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

InputText in DataTable does not store value

 
Pinal N Patel
Ranch Hand
Posts: 57
  • 0
  • Mark post as helpful
  • send pies
  • 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">
<tutputText 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">
<tutputText value="Calories" />
</f:facet>
<t:inputText id="caloriesId" size="4" value="#{food.calories}" />
</t:column>
<t:column>
<f:facet name="header" id="h3">
<tutputText value="Fat(Calories)" />
</f:facet>
<t:inputText id="fatCaloriesId" size="4"
value="#{food.fatCalories}" />
</t:column>
<t:column>
<f:facet name="header" id="h4">
<tutputText 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}"/>
<hutputText value=" " />
<h:commandButton value="Save Food Items" type="submit"
action="#{foodBean.executeFoodData}" />
<hutputText 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;
}
 
Anonymous
Ranch Hand
Posts: 18944
  • 0
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Richard Green
Ranch Hand
Posts: 536
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
whats the scope of your backing bean? request or session?
 
Pinal N Patel
Ranch Hand
Posts: 57
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've tried both and still no success :-)

Regards,
Pinal
 
C Porter
Greenhorn
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Ravindra Rawat
Ranch Hand
Posts: 34
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Klaus Schuster
Ranch Hand
Posts: 30
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic