I am using JSF to make an application to keep track of books but I am running into a lack of knowledge at this one page right now.
On startup of this page there is a textfield where you can enter search criteria and a search button. Below this is a message saying there is nothing to display. When the user presses the search button a datatable appears with the data of the relevant books.
The issue is that I don't know how to have no datatable in the beginning and update it everytime the user presses the search button. My code is as follows:
XHTML
<?xml version='1.0' encoding='ISO-8859-1' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<h:head>
<title>Library</title>
</h:head>
<h:body>
<h:form>
<h:outputLabel value="Libary"/>
<p/>
<div>
<h:outputLabel value="Search term" />
<h:inputText value="#{booksBean.filter}" />
<h:commandButton value="Search" action="#{booksBean.search}" />
</div>
<div>
<h:dataTable value="#{booksBean.books}" var="item">
<h:column>
<f:facet name="header">
<h:outputLabel value="Book Number"/>
</f:facet>
<h:commandLink value="#{item.bookNumber}" />
</h:column>
<h:column>
<f:facet name="header">
<h:outputLabel value="Title"/>
</f:facet>
<h:commandLink value="#{item.title}"/>
</h:column>
</h:dataTable>
</div>
</h:form>
</h:body>
</html>
Java
@ManagedBean
@ViewScoped
public class BooksBean implements Serializable{
private List<Book> books;
private String filter;
/**
* Creates a new instance of BooksBean
*/
public BooksBean () {
books = new ArrayList<Book>();
filter = "";
}
public List<Book> getBooks(){
List<Book> lijst;
list = new ArrayList<Book>();
Database database = new Database();
try {
list = database.getBooks(filter);
this.books = list;
} catch (SQLException ex) {
Logger.getLogger(BooksBean.class.getName()).log(Level.SEVERE, null, ex);
} catch (NamingException ex) {
Logger.getLogger(BooksBean.class.getName()).log(Level.SEVERE, null, ex);
}
return this.books;
}
public void search(){
}
public String getFilter() {
return filter;
}
public void setFilter(String filter) {
this.filter = filter;
}
}
Among my searches I couldn't find what functionality I could use, I've tried to use this answer, but I couldn't get it to work. Also I don't know if it then would update everytime a user would press the search button. I just started last week with JSF so I don't know how everything ties together.
Currently I feel like the database function might need to be in the "search" method instead of the getter. But apart from that I am quite lost after spending 2 hours on trying to understand how I would accomplish this. I would greatly appreciate some help.