Reputation: 237
For an assignment I must display a datatable of books pulled from a database, and that table must become editable on command so that the book entries in the database can be updated. My code for the table, which is based off an example our professor did, is as such:
<h:dataTable value="#{bookList.inventory}" var="book"
rowClasses="oddrow, evenrow" headerClass="header">
<h:column>
<f:facet name="header">ISBN</f:facet>
<h:inputText rendered="#{book.editable}" value="#{book.isbn}" />
<h:outputText rendered="#{not book.editable}"
value="#{book.isbn}" />
</h:column>
<h:column>
<f:facet name="header">Title</f:facet>
<h:inputText rendered="#{book.editable}" value="#{book.title}" />
<h:outputText rendered="#{not book.editable}"
value="#{book.title}" />
</h:column>
<h:column>
<f:facet name="header">Author</f:facet>
<h:inputText rendered="#{book.editable}" value="#{book.author}" />
<h:outputText rendered="#{not book.editable}"
value="#{book.author}" />
</h:column>
<h:column>
<f:facet name="header">Price</f:facet>
<h:inputText rendered="#{book.editable}" value="#{book.price}" />
<h:outputText rendered="#{not book.editable}"
value="#{book.price}" />
</h:column>
<h:column>
<h:commandLink
action="#{bookList.removeFromInventory(book)}"
value="Remove From Inventory">
<f:ajax render="@form" />
</h:commandLink>
</h:column>
<h:column>
<h:commandLink rendered="#{not book.editable}"
action="#{book.setEditable(not book.editable)}" value="Edit">
<f:ajax render="@form" />
</h:commandLink>
<h:commandLink rendered="#{book.editable}"
action="#{book.setEditable(not book.editable)}" value="Submit">
<f:actionListener binding="#{bookList.updateEntry(book)}" />
<f:ajax render="@form" />
</h:commandLink>
</h:column>
</h:dataTable>
And then updateEntry looks like this:
public void updateEntry(Book b) throws SQLException {
if(!b.getEditable()) return;
if(inventory == null) return;
if(ds == null) throw new SQLException("Can't connect to database");
try(Connection conn = ds.getConnection()){
PreparedStatement update = conn.prepareStatement("update books set "
+ "title='" + b.getTitle() + "', Author='" + b.getAuthor()
+ "', price=" + b.getPrice() + " where isbn=" +b.getIsbn());
update.execute();
}
}
The form successfully becomes editable, and when I click submit the updateEntry method is called correctly. However, it does not update the database correctly, because as I found through debugging for some reason it is not updating the variable book before calling the updateEntry methd. No matter what I put in the input it sends the same values to updateEntry. How can I make sure it updates the values in book before it calls the method?
Upvotes: 0
Views: 334
Reputation: 237
Found the solution after doing more research about when exactly JSF calls setter methods. Because I did not use the ajax attribute "execute" it did not call the setters before processing the request, because by default using only causes the element it is enclosed in to be processed before rendering the target.
The solution was to change my ajax tag to this:
<f:ajax execute="@form" render="@form" />
I hope this is able to help someone else trying to learn JSF!
Upvotes: 1