0

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.

Bob
  • 614
  • 1
  • 7
  • 19
  • When you click on the command button, JSF will call the method booksBean.search, this method should populates data to booksBean.books, see this example is about how to use dataTable component : https://www.mkyong.com/jsf2/jsf-2-datatable-example/ – La VloZ Merrill Dec 20 '16 at 15:41
  • take a step back and start with simple show/hide (render/not render) simple components with ajax. – Kukeltje Dec 20 '16 at 16:10
  • 1
    1) Use `rendered` attribute: ` – Artem Dec 22 '16 at 14:27
  • @Artem thanks, that worked great! – Bob Dec 27 '16 at 14:15

0 Answers0